Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

New to VBA, Please help!

Status
Not open for further replies.

dupas

Technical User
Aug 19, 2006
79
US
Here is what Im trying to do in Access:

I created a few command buttons that have the number 1, 2, 3, 4 on it, like a keypad or a calculator. When I press the button I want it to give a value (constant) of 1, 2, 3, 4, etc. At the same time if I press 1 and 4 on the keypad, Id like it to indicate 14 or if I press 9 and 8 I would get 98.

What I'm trying to do is:

commandButton1 = 10 * (number pad system stated above) + commandButtonEnter = 0

and/or

commandButton2 = 20 * (number pad system stated above) + commandButtonEnter = 0

and/or

commandButton 3 = 30 * (number pad system stated above) + commandButtonEnter = 0

= Total
 
Basically you're wanting to setup calc.exe like functionality in an Access form?

If so, you could have a Public variable, or possibly 2 or 3 variables, at the top of your VBA module, and then have those change according to the clicked buttons. So, something like this:

Option Explicit
Option Compare Database
Public lngInput1 As Long
Public lngInput2 As Long
Public lngOutput As Long

Then, you would have your procedures for each button, below:

Private Sub cmdButton1_Click()
If lngInput1 = vbNullString Then
lngInput1 = 1
Else
lngInput1 = lngInput1 & "1"
End If
End Sub

Then, something similar for each numeric button... and you'll need some more variations on that code - that's just the general idea, b/c you'll need to convert between string and long(numeric) I would think for some of this, if you want to be able to allow someone to enter 1111 by clicking the "1" button 4 times.

And You'll use you're operation buttons to assign what sort of calculations to use.

And thinking about it more, you'll probably need some additional critieria in each numeric button's procedure to check for whether an operation button was just clicked, and act accordingly. So, if the user has already set a value to the first input variable, and they have hit the "+" button, then you would assign the next value to the second input variable. So, you'll need an extra bit of code there.

I hope this makes some sense. I'm trying to just help with the idea, not necessarily just give you all the code you'll need..

--

"If to err is human, then I must be some kind of human!" -Me
 
That is the concept Im trying to do but I dont know how to do the code for it....
 
Where are you at, now? I mean, what do you have so far?

--

"If to err is human, then I must be some kind of human!" -Me
 
I've got a couple of thoughts of how I would go about it.

The basic idea is there in what I posted earlier..

So, I think you could go with it something like this:

First, name your buttons in a particular pattern. If you use the Reddick naming convention, that will help in part. Here is my suggestion on the naming of the controls. If they are all buttons:

cmdNum1
cmdNum2
cmdNum3
cmdNum4
cmdNum5
cmdNum6
cmdNum7
cmdNum8
cmdNum9
cmdNum0

cmdOpPlus
cmdOpMinus
cmdOpMult
cmdOpDiv

You can do whatever you like, but that is the first step - have some sort of scheme to the naming of your controls, so you don't get mixed up!

And you'll need a text box for the answer, at the least...

txtAnswer..


If I get a chance later, I'll check back, but gotta go, now.

--

"If to err is human, then I must be some kind of human!" -Me
 
One other thing...

Here is a link I found that may provide some thought. And I'd try a Google search for something like this, it appears there are at least several discussions about it, if not some real code/help.

--

"If to err is human, then I must be some kind of human!" -Me
 
This is my code......................... I wish I could give you an image on what I designed...

Option Compare Database

Private Sub cmdOne_Click()
txtAnswer = 1
End Sub

Private Sub cmdTwo_Click()
txtAnswer = 2
End Sub

Private Sub cmdThree_Click()
txtAnswer = 3
End Sub

Private Sub cmdFour_Click()
txtAnswer = 4
End Sub

Private Sub cmdFive_Click()
txtAnswer = 5
End Sub

Private Sub cmdSix_Click()
txtAnswer = 6
End Sub

Private Sub cmdSeven_Click()
txtAnswer = 7
End Sub

Private Sub cmdEight_Click()
txtAnswer = 8
End Sub

Private Sub cmdNine_Click()
txtAnswer = 9
End Sub

Private Sub cmdZero_Click()
txtAnswer = 0
End Sub

Private Sub cmdStdBurger_Click()
txtBurger = txtQtySBurger * 1.99
End Sub

Private Sub cmdDlxBurger_Click()
txtBurger = txtQtyDBurger * 2.99
End Sub

Private Sub cmdSmFries_Click()
txtFries = txtQtySFries * 0.99
End Sub

Private Sub cmdLrgFries_Click()
txtFries = txtQtyLFries * 1.49
End Sub

Private Sub cmdSmSoda_Click()
txtSoda = txtQtySSoda * 1.25
End Sub

Private Sub cmdLrgSoda_Click()
txtSoda = txtQtyLSoda * 1.75
End Sub

Private Sub cmdSubTotal_Click()
txtSubTotal = txtBurger + txtFries + txtSoda
End Sub

Private Sub cmdTotal_Click()
txtTax = txtSubTotal * 0.05
txtTotal = (txtSubTotal * 0.05) + txtSubTotal
End Sub

Private Sub cmdClrAll_Click()
txtSubTotal = "$0.00 "
txtTax = "$0.00 "
txtTotal = "$0.00 "
txtBurger = "$0.00 "
txtFries = "$0.00 "
txtSoda = "$0.00 "
txtQtySBurger = "0"
txtQtyDBurger = "0"
txtQtySFries = "0"
txtQtyLFries = "0"
txtQtySSoda = "0"
txtQtyLSoda = "0"
txtAmountRcvd = ""
txtChange = ""
If IsNull(txtOrder) Then
txtOrder = 1
Else
txtOrder = txtOrder + 1
End If
End Sub

Private Sub txtAmountRcvd_Enter()
txtAmountRcvd = ""
End Sub

Private Sub cmdEnter_Click()
txtAnswer = 0

'txtBurger = (txtQtySBurger * 1.99) + (txtQtyDBurger * 2.99)
'txtFries = (txtQtySFries * 0.99) + (txtQtyLFries * 1.49)
'txtSoda = (txtQtySSoda * 1.25) + (txtQtyLSoda * 1.75)
'txtSubTotal = txtBurger + txtFries + txtSoda
'txtTax = txtSubTotal * 0.05
'txtTotal = (txtSubTotal * 0.05) + txtSubTotal
End Sub

Private Sub cmdExit_Click()
DoCmd.Close acForm, "frmBurgerBarn"
End Sub
 
What Im trying to do is the following:

(standerdBurgerButton = 1.99) * (numeric pad situation) + ENTER = subtotal

and/or

(deluxeBurgerButton = 2.99) * (numeric pad situation) + ENTER = subtotal

and/or

(smallFriesButton = .99) * (numeric pad situation) + ENTER = subtotal

and/or

(largeFriesButton = 1.49) * (numeric pad situation) + ENTER = subtotal

and/or

(smallSodaButton = 1.25) * (numeric pad situation) + ENTER = subtotal

and/or

(largeSodaButton = 1.75) * (numeric pad situation) + ENTER = subtotal


salestax = subtotal * 0.05


total = salestax + subtotal


amountreceived = (numeric pad situation) + ENTER
amountreceived = ""


Private Sub cmdEnter_Click()

(i need to put code here for the numeric pad concept too?)

txtChange = txtAmountRcvd - txtTotal
End Sub



 
dupas,

I'm not able at the moment to look at this just yet, but I may be able to this afternoon (USA - Eastern Standard Time).

Where are you at so far, with this, if you've made progress? Have you run into any particular error messages or sticky situations?

Hopefully this afternoon, I'll be able to give this another look. [wink]

--

"If to err is human, then I must be some kind of human!" -Me
 




hi,

Just browsing thru your code and this jumped out.

When you hit a number on the pad, this is not the result...
Code:
Private Sub cmdOne_Click()
txtAnswer = 1
End Sub
Rather it concatenates the digit to the end of the existing string...
Code:
Private Sub cmdOne_Click()
txtAnswer = txtAnswer & "1"
End Sub
So another button you will need is a CLEAR button.

Skip,
[sub]
[glasses] [red][/red]
[tongue][/sub]
 
True... - what Skip said.

Also, dupas, you may want to not use txtAnswer for this portion of the code. If you are following the earlier thoughts, txtAnswer would actually be the name for a text box on your form.

You would want to use something more like strInput, I guess, as a String variable. I was thinking a Long (lngInput) variable, but I guess you really can't just concatenate a Long variable like you can a string variable. [blush]

So, you'd be better off (and you'll be able to read your code easier, I'd think) if you use strInput for the value storing all your inputs, and then in the end, you'll set the txtAnswer equal to all of your calculations.

So...
Code:
Private Sub cmdOne_Click()
   strInput = strInput & "1"
End Sub

Skip, would dupas need an If clause in the above code for times when there is no value already in strInput, or would there being no strInput value automatically take care of that possible problem?

In other words, I was thinking that it would have to be something like this:
Code:
Private Sub cmdOne_Click()
   If strInput = vbNullString Then
     strInput = "1"
   Else
     strInput = strInput & "1"
   End If
End Sub

So would some variation of that be required, or not necessary?

--

"If to err is human, then I must be some kind of human!" -Me
 
Thanks for all of your help... I got it to work by doing this...

Option Compare Database
Dim str As String

Private Sub cmdOne_Click()
str = str + "1"
txtAmountRcvd = str
End Sub

If you would like to see the whole code I can post it.
 
Just a suggestion, here. Though it is not a requirement, I highly recommend not just naming a variable, "str". Because if you go back later and have to sort through the code, it could become a little hard to follow - especially if there are ANY other parts to this code, such as if you add something later.

It's best to be descriptive with your variable names. Something like:


Name = strName
First Name = strFName
Last Name = strLName
Address = strAddress or strAdd

Just something that is more recognizeable to you, and any of those who might have to look at your code later - for instance, on the job, if you leave your post, and another fills that spot and has to edit something for whatever reason, or just simply needs to know what is going on in the code.

I hope you find the suggestion useful. I think some would go as far as to call that a law for programming. You can find plenty of info along these lines if you look for "naming convention" or even more specifically, the "Reddick Naming Convention"

--

"If to err is human, then I must be some kind of human!" -Me
 
I gotta ask........why? when there are various calculation programs and applications like excel ? What is it that you are trying to accomplish by building this ?

Rgds, Geoff

We could learn a lot from crayons. Some are sharp, some are pretty and some are dull. Some have weird names and all are different colours but they all live in the same box.

Please read FAQ222-2244 before you ask a question
 
xlbo,

That is indeed a good question. And it would be interesting to know a little background - out of curiosity.

--

"If to err is human, then I must be some kind of human!" -Me
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top