calling data from access

B

batuhan

i have the following code and gives the error "Run-time error: '-2147217900
(80040e14)': String error in 'ProductId ='

Sub GetData()

Const adOpenForwardOnly As Long = 0
Const adLockReadOnly As Long = 1
Const adCmdText As Long = 1
Dim oRS As Object
Dim sConnect As String
Dim sSQL As String
Dim ary

sConnect = " Provider = Microsoft.Jet.OLEDB.4.0; " & " Data Source= " &
"C:\Temp\product.mdb"
sSQL = "SELECT Price FROM Products WHERE ProductId = " &
Range("A10").Value
Set oRS = CreateObject("ADODB.Recordset")
oRS.Open sSQL, sConnect, adOpenForwardOnly, adLockReadOnly, adCmdText

If Not oRS.EOF Then
Sheet1.Range("C2").CopyFromRecordset oRS
Else
MsgBox (No data)
End If

oRS.Close
Set oRS = Nothing

End Sub

My db name = product.mdb
table name in my db = products
What i want to do is to call the value in 'price' column from my
'product.mdb' database to an excel sheet. My db columns are:

column1 = ProductId
column2 = ProductName
column3 = Price

My excel columns are named as

A1 = ProductId
B1 = ProductName
C1 = Price

For example, when i write ProductId to my A2 cell, it will call that
product's price from the db and put the value to C2 cell.

How can i correct the error?
 
N

NickHK

Going by the error ("String error in 'ProductId ="), I would assume you are
passing a string when it should be number.
Or if it should be a string, you are not enclosing it in quotes ;
sSQL = "SELECT Price FROM Products WHERE ProductId = " & Chr(34) &
Range("A10").Value & Chr(34)
Depends how your Products.ProductID column is defined.

NickHK
 
B

batuhan

All columns in products database are default, i've made no changes to their
definitions (number or string). The ID's consist of both numbers and
characters (like 1a.23 or 2c.44.65). The code you gave me made a 'type
mismatch' error. Any other ideas? (Maybe a different solution from my code).

If it will be easier, i may import my db to another sheet and call data from
excel to excel, but i'll need the code to do that
 
N

NickHK

Do Debug.Print sSQL
What do you get ?

NickHK

batuhan said:
All columns in products database are default, i've made no changes to
their
definitions (number or string). The ID's consist of both numbers and
characters (like 1a.23 or 2c.44.65). The code you gave me made a 'type
mismatch' error. Any other ideas? (Maybe a different solution from my
code).

If it will be easier, i may import my db to another sheet and call data
from
excel to excel, but i'll need the code to do that
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top