Searching a table for a value using Query in VBA

Challseus

Board Regular
Joined
Feb 5, 2003
Messages
141
Hi all:

I want to be able to search a database for a specific value, and if it isn't there output an error message. I want to be able to do all this in VBA. Is there some sort of Query function or something like this?

Any help would be much appreciated.
 

Excel Facts

Copy a format multiple times
Select a formatted range. Double-click the Format Painter (left side of Home tab). You can paste formatting multiple times. Esc to stop
Search Help for the Database functions (DLookup, in particular).

Let us know if you are still having trouble after you have tried this...

-Russell
 
Upvote 0
I'm not a query expert but I play one on TV but I can do this kind of thing "IN" Access; at any rate, you have a better chance asking this on the Excel side. Try the Excel help item "About importing data" too.

If you're brave you can try analyzing the code produced by this in Excel (turn on "record macro"):
file/open/select filetype Microsoft Access/pick a Select query

This will link up to an existing query in an Access database. It would produce a column of database query results in e.g. A1 through {however many rows are needed, to show matches to your "specific value"}. You could then have your VBA compare A1 to "" or Empty or whatever. Or just use a cell directly, a la
if(A1="",":)","Error msg")

I hope I got your drift with this question.

At any rate, this may be more trouble than for what you want. As the prior post indicates, use dlookup, which specifically tells you whether an item exists in a database or not -- as a Boolean answer.

DCOUNT may also right your flight.
 
Upvote 0
I disagree about asking on the Excel side. This is not at all an Excel question. DCount or DLookup will do the trick:
Code:
If IsNull(DLookup("tblMine","[SomeField]","[Somefield] = 'Bill'") Then
    'Error Message
End If

'or

If DCount("tblMine","[Somefield]","[Somefield = 'Bill'") = 0 Then
    'Error Message
End If
HTH,

Russell
 
Upvote 0
Well I believed it to be a stretch for this thin forum to give VBA-in-Excel answers. There are tons of Access users in the Excel forum - and some of them quite expert on that front.
 
Upvote 0
Sorrry I did not get back to you guys last night, but my home PC's keyboard is screwed up. In fact, only half the letters on it work. But that's besides the point...I appreciate the fast answer and I'm looking forward to trying it out at work this afternoon. I'll keep you guys posted as to if it works or not. Again, your help is much appreciated.
 
Upvote 0
Okay, I'm still having probelms. Here's my code:

Code:
If IsNull(DLookup("Students", "[ssn]", "[ssn] = 'Bill'")) Then
    MsgBox "Not Found"
End If

Now am I doing this right? "Students" is the name of the table I want to Query. "ssn" is the field I want to look at. Finally, "Bill" is the name I'm looking for.

I'm getting an error along the lines of cannot find table or query 'ssn'. Is the table private and I have to use an accessor function?
 
Upvote 0
Challseus said:
Okay, I'm still having probelms. Here's my code:

Code:
If IsNull(DLookup("Students", "[ssn]", "[ssn] = 'Bill'")) Then
    MsgBox "Not Found"
End If

Now am I doing this right? "Students" is the name of the table I want to Query. "ssn" is the field I want to look at. Finally, "Bill" is the name I'm looking for.

I'm getting an error along the lines of cannot find table or query 'ssn'. Is the table private and I have to use an accessor function?
Probably should be something like:
Code:
If IsNull(DLookup("Students", "[ssn]", "[name] = 'Bill'")) Then
    MsgBox "Not Found"
End If
Where "name" is the name of the field where "Bill" will be found.

HTH,

Russell
 
Upvote 0
Thx, that helped out, except I changed this:
Code:
If IsNull(DLookup("Students", "[ssn]", "[ssn] = 'Bill'")) Then 
    MsgBox "Not Found" 
End If
To this:
Code:
If IsNull(DLookup("ssn", "Students", "[ssn] = 'Bill'")) Then 
    MsgBox "Not Found" 
End If
I truly appreciate your help.
 
Upvote 0

Forum statistics

Threads
1,221,539
Messages
6,160,412
Members
451,644
Latest member
hglymph

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