Syntax error for vlookup function from access database

baha17

Board Regular
Joined
May 12, 2010
Messages
181
Hi All,
I found one vllokup function from access data base in earlier posts. Thanks to the author. However, when I tried to adop in my userform as it gets the password line form access data base. But I am getting an syntax error.The error occurs in
Code:
adoRS.Open Source:=strSQL, ActiveConnection:=adoCN, _
             CursorType:=adOpenDynamic, LockType:=adLockOptimistic, _
             Options:=adCmdTable
Before I alter the code I tested and it was working. My database has password:XXX.
to cut the story short i put the sample as msgbox

Is there anyone can help me?
Thanks for the hand
Code:
Option Explicit
Const TARGET_DB4 = "CPUsersDB.mdb"
Dim adoCN As ADODB.Connection
Dim strSQL As String
Public Function DBVLookUp(TableName As String, LookUpFieldName As String, LookupValue As String, ReturnField As String) As Variant
Dim adoRS As ADODB.Recordset
'If adoCN Is Nothing Then SetUpConnection
Dim MyConn
    MyConn = ThisWorkbook.Path & Application.PathSeparator & TARGET_DB4
Dim adoCN As ADODB.Connection
Set adoCN = New Connection
'adoCN.Provider = "Microsoft.Jet.OLEDB.4.0"  'Change to 3.51 for Access 97
'adoCN.ConnectionString = "K:financemanagement reportingfinance reporting 2002.mdb" 'Change to your DB path
adoCN.Open "Provider=Microsoft.Jet.OLEDB.4.0;" _
                & "Data Source=" & MyConn & ";" _
                & "Jet OLEDB:Database Password=XXX;"
Set adoRS = New ADODB.Recordset
adoRS.CursorLocation = adUseServer
strSQL = "SELECT " & LookUpFieldName & ", " & ReturnField & " FROM " & TableName & " WHERE " & LookUpFieldName & _
         "='" & LookupValue & "';"    ' If lookup value is a number then remove the two '
adoRS.Open Source:=strSQL, ActiveConnection:=adoCN, _
             CursorType:=adOpenDynamic, LockType:=adLockOptimistic, _
             Options:=adCmdTable
'strSQL, adoCN, adOpenForwardOnly, adLockReadOnly
    
DBVLookUp = adoRS.Fields(ReturnField).Value
adoRS.Close
End Function
Sub susak()
MsgBox DBVLookUp("tblCPUser", "TMId", 5378, "Password")
'"SELECT * FROM tblAllocation WHERE TblID = " & lngID
End Sub
 

Excel Facts

Test for Multiple Conditions in IF?
Use AND(test, test, test, test) or OR(test, test, test, ...) as the logical_test argument of IF.
I am sorry to replying my own thread but I figured out the error. LookupValue type was wrong in the database. I changed it to "adVarWChar, 18" instead of "Double" it worked fine. Sorry for taking your time.
Thanks
 
Upvote 0

Forum statistics

Threads
1,215,059
Messages
6,122,918
Members
449,093
Latest member
dbomb1414

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