VBA calculator problems

Problems coding VBA calculator

  • (2 Pages)
  • +
  • 1
  • 2

20 Replies - 4179 Views - Last Post: 15 May 2009 - 10:05 PM Rate Topic: -----

#1 chicodjt  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 12-May 09

VBA calculator problems

Post icon  Posted 12 May 2009 - 03:36 PM

I am having problems coding the + - * / buttons in my calculator in VBA. When I hit the equals button, nothing happens, however my code looks right. Can someone help me out?


Private Sub Clear_Click()
screen.Text = "0"

End Sub

Private Sub negative_Click()
screen.Text = screen.Text * (-1)

End Sub

Private Sub num0_Click()
screen.Text = screen.Text + "0"

End Sub

Private Sub num1_Click()
screen.Text = screen.Text + "1"

End Sub

Private Sub num2_Click()
screen.Text = screen.Text + "2"

End Sub


Private Sub num3_Click()
screen.Text = screen.Text + "3"

End Sub

Private Sub num4_Click()
screen.Text = screen.Text + "4"

End Sub

Private Sub num5_Click()
screen.Text = screen.Text + "5"

End Sub

Private Sub num6_Click()
screen.Text = screen.Text + "6"

End Sub

Private Sub num7_Click()
screen.Text = screen.Text + "7"

End Sub

Private Sub num8_Click()
screen.Text = screen.Text + "8"

End Sub

Private Sub num9_Click()
screen.Text = screen.Text + "9"

End Sub



Private Sub point_Click()
screen.Text = screen.Text + "."

End Sub
Private Sub plus_Click()
screen.Text = a
screen.Text = "+"
Sign = plus


End Sub

Private Sub minus_Click()
screen.Text = a
screen.Text = "-"
Sign = minus

End Sub
Private Sub multiply_Click()
screen.Text = a
screen.Text = "*"
Sign = times
End Sub
Private Sub divide_Click()
screen.Text = a
screen.Text = "/"
Sign = divide

End Sub
Private Sub equals_Click()
b = screen.Text
		If Sign = "minus" Then
			 screen.Text = a - b
		ElseIf Sign = "plus" Then
			 screen.Text = a + b
		ElseIf Sign = "times" Then
			 screen.Text = a * b
		ElseIf Sign = "divide" Then
			 screen.Text = a / b
		End If

End Sub

Private Sub Square_Click()
a = screen.Text
screen.Text = "^2"
c = a * a
screen.Text = c

End Sub



Mod Edit: Please use code tags when posting your code. Code tags are used like so => :code:

Thanks,
PsychoCoder :)

Is This A Good Question/Topic? 0
  • +

Replies To: VBA calculator problems

#2 June7  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 104
  • View blog
  • Posts: 904
  • Joined: 09-December 08

Re: VBA calculator problems

Posted 12 May 2009 - 04:36 PM

Have you declared the variable Sign as public? Also think need to put the words: plus, minus, times, divide inside quotes (Sign = "plus") in their respective click events.

This post has been edited by June7: 12 May 2009 - 04:39 PM

Was This Post Helpful? 1

#3 thava  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 180
  • View blog
  • Posts: 1,606
  • Joined: 17-April 07

Re: VBA calculator problems

Posted 12 May 2009 - 06:39 PM

there is a problem in your code where did you assign the vallue for the variable a change the code like this


is the variable sign is string or var
declare the varable sign as string

Private Sub plus_Click()
'screen.Text = a				   ' Your code
a =val(screen.Text ) 
screen.Text = "+"
'Sign = plus				  ' Your code
Sign = "plus"

End Sub




it is always a good practice use the option explicit statement in your form

This post has been edited by thava: 12 May 2009 - 06:44 PM

Was This Post Helpful? 0
  • +
  • -

#4 chicodjt  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 12-May 09

Re: VBA calculator problems

Posted 12 May 2009 - 08:27 PM

First off, thanks for the help so far. I tried the suggestions and still nothing happens when i hit the = button. here's what i have now:


Private Sub Clear_Click()
screen.Text = "0"

End Sub








Private Sub negative_Click()
screen.Text = screen.Text * (-1)

End Sub

Private Sub num0_Click()
screen.Text = screen.Text + "0"

End Sub

Private Sub num1_Click()
screen.Text = screen.Text + "1"

End Sub

Private Sub num2_Click()
screen.Text = screen.Text + "2"

End Sub


Private Sub num3_Click()
screen.Text = screen.Text + "3"

End Sub

Private Sub num4_Click()
screen.Text = screen.Text + "4"

End Sub

Private Sub num5_Click()
screen.Text = screen.Text + "5"

End Sub

Private Sub num6_Click()
screen.Text = screen.Text + "6"

End Sub

Private Sub num7_Click()
screen.Text = screen.Text + "7"

End Sub

Private Sub num8_Click()
screen.Text = screen.Text + "8"

End Sub

Private Sub num9_Click()
screen.Text = screen.Text + "9"

End Sub



Private Sub point_Click()
screen.Text = screen.Text + "."

End Sub
Private Sub plus_Click()
a = Val(screen.Text)
screen.Text = "+"
Sign = "plus"


End Sub

Private Sub minus_Click()
a = Val(screen.Text)
screen.Text = "-"
Sign = "minus"

End Sub
Private Sub multiply_Click()
a = Val(screen.Text)
screen.Text = "*"
Sign = "times"

End Sub

Private Sub divide_Click()
a = Val(screen.Text)
screen.Text = "/"
Sign = "divide"

End Sub
Private Sub equals_Click()
b = Val(screen.Text)
		If Sign = "minus" Then
			 screen.Text = a - b
		ElseIf Sign = "plus" Then
			 screen.Text = a + b
		ElseIf Sign = "times" Then
			 screen.Text = a * b
		ElseIf Sign = "divide" Then
			 screen.Text = a / b
		End If

End Sub

Private Sub Square_Click()
a = screen.Text
screen.Text = "^2"
c = a * a
screen.Text = c

End Sub


Was This Post Helpful? 0
  • +
  • -

#5 firebolt  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 92
  • View blog
  • Posts: 5,561
  • Joined: 20-February 09

Re: VBA calculator problems

Posted 12 May 2009 - 10:48 PM

declare "sign"
Was This Post Helpful? 0
  • +
  • -

#6 thava  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 180
  • View blog
  • Posts: 1,606
  • Joined: 17-April 07

Re: VBA calculator problems

Posted 12 May 2009 - 10:54 PM

Private Sub plus_Click()
'screen.Text = a				   ' Your code
a =val(screen.Text ) 
screen.Text = "0"
'Sign = plus				  ' Your code
Sign = "plus"

End Sub



and declare the variable sign as form level string
like
Dim sign as string


that's all it's works for me

This post has been edited by thava: 12 May 2009 - 10:57 PM

Was This Post Helpful? 0
  • +
  • -

#7 chicodjt  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 12-May 09

Re: VBA calculator problems

Posted 12 May 2009 - 11:17 PM

View Postfirebolt, on 12 May, 2009 - 09:48 PM, said:

declare "sign"


declare it as what?
Was This Post Helpful? 0
  • +
  • -

#8 June7  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 104
  • View blog
  • Posts: 904
  • Joined: 09-December 08

Re: VBA calculator problems

Posted 12 May 2009 - 11:32 PM

You are trying to use variables that are set in another procedure. You must 'pass' the variable to the using procedure or declare it as public. Since this code is behind a form, declare public variables in the form's code header area. Variables 'Sign' and 'a' fit this situation. Use step debugging to follow the code execution. The code will stop after each line of code is executed. You can put breakpoints where you think the problem is and code will run until the breakpoint is encountered and then will be in step mode. This allows you to see what values the variables hold. Just hover the cursor over them. Suggest you do this with each click event. You will get a better understanding of how your program works.

Edit: You identify this as VBA, are you in an Access project? Scratch question, your code looks like VB.Net

In VBA for Accesss would declare these variables with the Dim statement as described in post above in header section of form. May be somewhat different in VB.Net. But must be declared as public so that all the click procedures manipulating them can get the value set in other procedures. Is this clearer now?

This post has been edited by June7: 12 May 2009 - 11:44 PM

Was This Post Helpful? 0
  • +
  • -

#9 chicodjt  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 12-May 09

Re: VBA calculator problems

Posted 13 May 2009 - 12:09 PM

I obviously didn't do it right, since it's still not working. by the way, thanks for the help so far.



Private Sub Clear_Click()
screen.Text = "0"

End Sub








Private Sub negative_Click()
screen.Text = screen.Text * (-1)

End Sub

Private Sub num0_Click()
screen.Text = screen.Text + "0"

End Sub

Private Sub num1_Click()
screen.Text = screen.Text + "1"

End Sub

Private Sub num2_Click()
screen.Text = screen.Text + "2"

End Sub


Private Sub num3_Click()
screen.Text = screen.Text + "3"

End Sub

Private Sub num4_Click()
screen.Text = screen.Text + "4"

End Sub

Private Sub num5_Click()
screen.Text = screen.Text + "5"

End Sub

Private Sub num6_Click()
screen.Text = screen.Text + "6"

End Sub

Private Sub num7_Click()
screen.Text = screen.Text + "7"

End Sub

Private Sub num8_Click()
screen.Text = screen.Text + "8"

End Sub

Private Sub num9_Click()
screen.Text = screen.Text + "9"

End Sub



Private Sub point_Click()
screen.Text = screen.Text + "."

End Sub
Public Sub plus_Click()
screen.Text = "a"
screen.Text = "+"
Dim sign As String

sign = "plus"



End Sub

Public Sub minus_Click()
screen.Text = "a"
screen.Text = "-"
Dim sign As String

sign = "minus"



End Sub
Public Sub multiply_Click()
screen.Text = "a"
screen.Text = "*"
Dim sign As String

sign = "times"



End Sub
Public Sub divide_Click()
screen.Text = "a"
screen.Text = "/"
Dim sign As String

sign = "divide"



End Sub
Private Sub equals_Click()
b = screen.Text
		If sign = "minus" Then
			 screen.Text = a - b
		ElseIf sign = "plus" Then
			 screen.Text = a + b
		ElseIf sign = "times" Then
			 screen.Text = a * b
		ElseIf sign = "divide" Then
			 screen.Text = a / b
		End If

End Sub

Private Sub Square_Click()
a = screen.Text
screen.Text = "^2"
c = a * a
screen.Text = c

End Sub



Was This Post Helpful? 0
  • +
  • -

#10 June7  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 104
  • View blog
  • Posts: 904
  • Joined: 09-December 08

Re: VBA calculator problems

Posted 13 May 2009 - 03:27 PM

In what way is it not working now. Have you step debugged? Declaring 'Sign' in each procedure won't work. It and 'a' have to be declared in public area, as I said, for Access that would be the header of the code behind the form. I don't use VB.Net but gleaned from book would be in a module or form class, something like:
Public Class Form1

  Public Sign As String
  Public a As String

  'continue with code for your form controls

End Class

This post has been edited by June7: 13 May 2009 - 03:31 PM

Was This Post Helpful? 0
  • +
  • -

#11 chicodjt  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 12-May 09

Re: VBA calculator problems

Posted 13 May 2009 - 03:31 PM

View PostJune7, on 13 May, 2009 - 02:27 PM, said:

In what way is it not working now. Have you step debugged? Declaring 'Sign' in each procedure won't work. It and 'a' have to be declared in public area, as I said, for Access that would be the header of the code behind the form. I don't use VB.Net but gleaned from book would be in a module or form class, something like:
Public Class Form1

  Public Sign As String

  'continue with code for your form controls

End Class


I'm not using vb.net. It won't work meaning nothing happens when i hit the equals button. Could you please explain step debugging?
Was This Post Helpful? 0
  • +
  • -

#12 June7  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 104
  • View blog
  • Posts: 904
  • Joined: 09-December 08

Re: VBA calculator problems

Posted 13 May 2009 - 03:41 PM

Are you using Access? If you are, then on the VBA code editor menu is Debug. Query the Help for Debug Menu for info on how to use these tools.

Also, check out.
http://www.fmsinc.co...ps/VBA/VBA1.asp

This post has been edited by June7: 13 May 2009 - 03:42 PM

Was This Post Helpful? 0
  • +
  • -

#13 chicodjt  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 12-May 09

Re: VBA calculator problems

Posted 13 May 2009 - 03:46 PM

View PostJune7, on 13 May, 2009 - 02:41 PM, said:

Are you using Access? If you are, then on the VBA code editor menu is Debug. Query the Help for Debug Menu for info on how to use these tools.

Also, check out.
http://www.fmsinc.co...ps/VBA/VBA1.asp


ok. I still don't see why it wouldn't work though, i declared it as public, dimentioned it as a string, and put quotes around the sign.
Was This Post Helpful? 0
  • +
  • -

#14 June7  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 104
  • View blog
  • Posts: 904
  • Joined: 09-December 08

Re: VBA calculator problems

Posted 13 May 2009 - 03:52 PM

Still trying to determine what application you are working in. You said VBA code, that would mean Access, Excel, or Word. In Access to get the info entered into a text box would use .Value instead of .Text. Is 'screen' a text box?
Was This Post Helpful? 0
  • +
  • -

#15 chicodjt  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 12-May 09

Re: VBA calculator problems

Posted 13 May 2009 - 03:54 PM

View PostJune7, on 13 May, 2009 - 02:52 PM, said:

Still trying to determine what application you are working in. You said VBA code, that would mean Access, Excel, or Word. In Access to get the info entered into a text box would use .Value instead of .Text. Is 'screen' a text box?


o, excell. screen is a textbox. i just renamed it screen.
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2