VBA to compare variable to data in a field

MikeLiberty

Board Regular
Joined
Aug 13, 2010
Messages
55
Hi,
I know a little VBA from my work with Excel, but I'm having trouble moving over into Access. I'm trying to find a way to determine if the value of a variable exists within a table. How do I compare a variable to all of the rows in a field. If I was in excel, I'd just loop through with for each or something similar but I'm not sure what the syntax is for doing it in Access.

Can anyone help me? I'm envisioning something like this.

For each ? in table.field
if ? = myvariable then
execute code
end if
Next ?



Thanks,
mike
 
Let's take a step back and confirm a few things first.

Is "Check" the name of a field in your table?
Also, is your "PIN" field numeric or text?

Check is not a field in the table. Its just Username and PIN. the PIN field is text as is the value of the UserPin variable.
 
Upvote 0

Excel Facts

Excel Can Read to You
Customize Quick Access Toolbar. From All Commands, add Speak Cells or Speak Cells on Enter to QAT. Select cells. Press Speak Cells.
Not sure why you had "Check" in your formula then.

Anyway, this is what your DCOUNT code would look like:
Code:
    UserPin = "1234"
    LTotal = DCount("UserName", "UserVerification", "PIN = '" & UserPin & "'")
    MsgBox LTotal

To lookup and return the UserName associated with it, the code would look something like this, though you might need to add error handling code to it to tell it what to do in the event that the id is not found.
Code:
    UserPin = "1234"
    LUserName = DLookup("UserName", "UserVerification", "PIN = '" & UserPin & "'")
    MsgBox LUserName
 
Upvote 0
That worked. Thanks for walking me through all of that. I appreciate it. I also used the Lookup code you added to generate a welcome message.

Thanks Again!
 
Upvote 0
Your welcome! Glad we got it all worked out.
:cool:
 
Upvote 0

Forum statistics

Threads
1,224,522
Messages
6,179,292
Members
452,902
Latest member
Knuddeluff

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top