I am writing some Excel VBA to retrive a value from an Access Database called SSI20031.mdb.
In a nutshell I want the code to look at the value in Sheet1 cell F55 of the excel spreadsheet and then find the record that has this value in the "ID" field of query called "Code Generator" of the database. After finding the appropriate record I then want the code to store the value in the "Expr1" field of the database for the selected record and pass this into field G55 of the Excel Sheet1.
I have written the following code:
Sub ReturnCode()
Dim cnnz As ADODB.Connection
Dim rsz As ADODB.Recordset
Dim sSQLz As String, strConnz
Dim Numbeb As Variant
'Q:\IT\Database Masters\Guarantees2.mdb
'
strConnz = "Provider=Microsoft.Jet.OLEDB.4.0; " _
& "Data Source=Q:\IT\Database Masters\SSI20031.mdb;Persist Security Info=False"
Set cnnz = New ADODB.Connection
cnnz.Open strConnz
Set Numbeb = Worksheets("Sheet1").Range("F55").Value
'sSQLz = "SELECT [Code Generator].* FROM [Code Generator] WHERE [Code Generator].ID='" & Range("F55").Value & "';"
sSQLz = "SELECT [Code Generator].* FROM [Code Generator] WHERE [Code Generator].ID=Numbeb;"
Set rsz = New ADODB.Recordset
rsz.Open sSQLz, cnnz, adOpenStatic, adLockOptimistic
If Not IsNull(rsz.Fields("ID").Value) Then
Range("G55") = rsz.Fields("Expr1").Value
End If
rsz.Close
Set rsz = Nothing
cnnz.Close
Set cnnz = Nothing
End Sub
I keep getting a type mismatch message on line
"Set Numbeb = Worksheets("Sheet1").Range("F55").Value"
Field F55 only carries numbers e.g. 1680 - so to me I am not sure why this is an issue. The ID field in the database is using autonumber - so that should be a number also. Any ideas on what I need to do to fix this?
thanks
In a nutshell I want the code to look at the value in Sheet1 cell F55 of the excel spreadsheet and then find the record that has this value in the "ID" field of query called "Code Generator" of the database. After finding the appropriate record I then want the code to store the value in the "Expr1" field of the database for the selected record and pass this into field G55 of the Excel Sheet1.
I have written the following code:
Sub ReturnCode()
Dim cnnz As ADODB.Connection
Dim rsz As ADODB.Recordset
Dim sSQLz As String, strConnz
Dim Numbeb As Variant
'Q:\IT\Database Masters\Guarantees2.mdb
'
strConnz = "Provider=Microsoft.Jet.OLEDB.4.0; " _
& "Data Source=Q:\IT\Database Masters\SSI20031.mdb;Persist Security Info=False"
Set cnnz = New ADODB.Connection
cnnz.Open strConnz
Set Numbeb = Worksheets("Sheet1").Range("F55").Value
'sSQLz = "SELECT [Code Generator].* FROM [Code Generator] WHERE [Code Generator].ID='" & Range("F55").Value & "';"
sSQLz = "SELECT [Code Generator].* FROM [Code Generator] WHERE [Code Generator].ID=Numbeb;"
Set rsz = New ADODB.Recordset
rsz.Open sSQLz, cnnz, adOpenStatic, adLockOptimistic
If Not IsNull(rsz.Fields("ID").Value) Then
Range("G55") = rsz.Fields("Expr1").Value
End If
rsz.Close
Set rsz = Nothing
cnnz.Close
Set cnnz = Nothing
End Sub
I keep getting a type mismatch message on line
"Set Numbeb = Worksheets("Sheet1").Range("F55").Value"
Field F55 only carries numbers e.g. 1680 - so to me I am not sure why this is an issue. The ID field in the database is using autonumber - so that should be a number also. Any ideas on what I need to do to fix this?
thanks