VBA for simple query

A

AccessMan

I have a form with an unbound combo box into which the user enters a value
and then clicks a button. I would like to use VBA on the button's On Click
event to determine if the entered value matches a record in a table by virtue
of comparison to the values in the table's single field key. In general, I
need to execute an sql query with VBA and determine if the results are null
or not null. Are there any VBA statements/commands available for such
purposes?

Thanks!
 
D

Douglas J. Steele

Simplest would be to use the DLookup function:

If IsNull(DLookup("[FieldName]", "[TableName]", _
"ID = " & Me!MyComboBox)) Then
' The record doesn't exist
Else
' The record exists
End If
 
F

Frank

And one might put DJS's suggested code in the After Update event of the Combo
Box rather than use a separare Command Button ...

Douglas J. Steele said:
Simplest would be to use the DLookup function:

If IsNull(DLookup("[FieldName]", "[TableName]", _
"ID = " & Me!MyComboBox)) Then
' The record doesn't exist
Else
' The record exists
End If



--
Doug Steele, Microsoft Access MVP

(no e-mails, please!)


AccessMan said:
I have a form with an unbound combo box into which the user enters a value
and then clicks a button. I would like to use VBA on the button's On
Click
event to determine if the entered value matches a record in a table by
virtue
of comparison to the values in the table's single field key. In general,
I
need to execute an sql query with VBA and determine if the results are
null
or not null. Are there any VBA statements/commands available for such
purposes?

Thanks!
 
F

Frank

Or, better yet, use a query on the very table as the Row Source for the Combo
Box and set the CB's "Limit to List" property to True ... your operator can
then pick an item in the CB with the mouse (rather than have to type) and she
won't be _able_ to put a value not in the table into the CB.

Frank said:
And one might put DJS's suggested code in the After Update event of the Combo
Box rather than use a separare Command Button ...

Douglas J. Steele said:
Simplest would be to use the DLookup function:

If IsNull(DLookup("[FieldName]", "[TableName]", _
"ID = " & Me!MyComboBox)) Then
' The record doesn't exist
Else
' The record exists
End If



--
Doug Steele, Microsoft Access MVP

(no e-mails, please!)


AccessMan said:
I have a form with an unbound combo box into which the user enters a value
and then clicks a button. I would like to use VBA on the button's On
Click
event to determine if the entered value matches a record in a table by
virtue
of comparison to the values in the table's single field key. In general,
I
need to execute an sql query with VBA and determine if the results are
null
or not null. Are there any VBA statements/commands available for such
purposes?

Thanks!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top