Hi Richard,
I will give your question a shot. First, I am assuming that the textbox
you are referring to is a textbox on a form into which you want to enter
a number - and ID number perhaps, and you want to check if there is a
record in a table with that ID - and I assume that the table is not a
recordsource for the form. My answer to your question - which I
interpret to be "how is this done?" will be based on the assumptions I
have stated. Note: if you scenario is different than what I have
described, you need to specify what your scenario is.
So, in the form that contains your textbox - call it Text0, you can add
a command button. In the command button click event you can add this
code:
Private Sub Command4_Click()
Dim i As Integer
On Error Resume Next
i = DLookup("ID", "Table1", "ID = " & Text0)
If i <0 Then MsgBox "Yes exists"
If i = 0 Then MsgBox "Not exists"
End Sub
This is a real basic searcher using the Access builtin DLookUp function.
The DLookUp function has to be set equal to something. Here I am
assuming your table has an ID field (the key field) which would be an
integer (but it could be alpha numeric also - just my example is using
an integer). So I have a variable i, which is defined as an integer.
The DLookup function takes 3 argument - the field you want to search,
the table which contains this field, and the criteria for your search.
"ID", "Table1", "ID = " & Text0
Note: when I was starting out with Access the Tooltip help text for the
Dlookup function always used to confuse me. It would stay "Expr As
String", "Domain As String", "Criteria As String". I could figure out
Criteria, but Expr means the field you are searching on. Domain means
the table (in VB.Net they made things way more Plain English - it is
just OOP based).
For the criteria I have "ID = " & Text0. I could have hardcoded the
value for the criteria
"ID = 12345", but then you can't change the criteria. So you specify
part of the criteria inside the quotes
"ID = "
then you concatenate the variable part Text0 using the & ampersand
symbol. Text0 can be anything, 1, 2, 3, 123, 12345, abc...Note: when
using chars for the criteria you have to delimit chars with single
quotes. Now your criteria would look like this:
"ID = '" & Text0 & "'"
You don't have to do this for numbers. And for dates you delimit dates
with the # pound symbol.
"ID = #" & txtDate & "#"
Then I also use the statement "ON Error Resume Next". If the value you
are searching does not exist, DLookUp will throw an error. If it throws
an error you trap the error using the On Error statement and then tell
it what to do - Go to a label "On Error GoTo lblErr" or just ignore the
error "On Error Resume Next". Here I ignore the error and just check if
i received a value or not. If yes - state yes If i <0 then MsgBox
"Yes Exists"
The only downside with this simple example is if you are searching for
0. The problem is that i will be 0 by default. If you don't find a 0,
i will still be 0. A more sophisticated version of this example would
be a boolean search or you could change the error trap to go to a lable
"on error goto errlbl
Private Sub Command4_Click()
Dim i As Integer
On Error GoTo Errlbl
i = DLookup("ID", "Table1", "ID = " & Text0)
MsgBox "Yes Exists"
Exit Sub
Errlbl:
MsgBox "Not Exists"
End Sub
This example will tell you if there is no 0 because you will get an
error - then go to the Errlbl. If the number does exist then it doesn't
go to the Errlbl - if you did not forget to use the "Exit Sub" statement
before the Errlbl. If you omit the "Exit Sub" statement then even if
there was no error the code will still go to the Errlbl because you did
not exit the subroutine yet. Whenever you use On Error GoTo someLabel -
you must always include "Exit Sub". Also, when using a lable, you have
to follow it with a : semicolon.
HTH
Rich
*** Sent via Developersdex
http://www.developersdex.com ***