DLookup Using Variable

Gregorys05

Board Regular
Joined
Sep 24, 2008
Messages
217
Hi All,
I have the below code that i am trying to referance a query within a Dlookup, now the code works fine when i enter the name of the query into the dlookup(Instead of the SQL) but when using the SQL variable it dosn't recognise that its a query what do i have to do for the Dlookup to recognise it??

Thanks

Code:
Private Sub Add_Click()
Dim VersionName As String
Dim SQL As String
VersionName = Forms!Replace!NewImageaddress & "V1"
SQL = "SELECT tblreplace.fldScanName FROM tblreplace WHERE (((tblreplace.fldScanName = " & VersionName & "));"
If DLookup(1, SQL, True) Then
MsgBox "Value there"
Else
MsgBox "Not There"
End If
End Sub
 

Excel Facts

Can you sort left to right?
To sort left-to-right, use the Sort dialog box. Click Options. Choose "Sort left to right"
You can't use SQL for the 2nd argument of a DLookUp it has to be the name of a table or query.

One thing you could try would be to create a parameter query out of your SQL.

That shouldn't be too hard, just use Forms!Replace!NewImageaddress & "V1" in the criteria.

Or if all you want to do is find out if the value from the form is already in the fldScanName field you could use DCount.

If that returns 0 it's not in the table.

Something like this perhaps:

=DCount("[fldScanName]", "tblreplace", "[fldScanName]='" & Forms!Replace!NewImageaddress & "V1'")
 
Upvote 0

Forum statistics

Threads
1,224,607
Messages
6,179,871
Members
452,948
Latest member
UsmanAli786

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