VBA Excel get data from Access database Error

0 votes

Hi, I have a problem running the VBA code in excel to get data from access get Error code runtime error: -2147217900 "Invalid SQL statement when check to debug have a problem on code rst.Open "Production Result", con  when I change another table in same database file not error. rst.Open "Production cost", con

Sub getdata()
    Dim con As ADODB.Connection
    Set con = New ADODB.Connection
    con.Open ("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=\\C:Mydata\database.accdb;")
    Dim rst As ADODB.Recordset
    Set rst = New ADODB.Recordset
    rst.Open "Production Result", con
    Dim r As Long
    r = 3
    rst.MoveFirst
    Do While Not rst.EOF
        Sheets("sheet1").Cells(r, 3).Value = rst.Fields("lot_no").Value
        r = r + 1
        rst.MoveNext
    Loop
End Sub
Mar 19 in Others by narikkadan
• 60,820 points
30 views

1 answer to this question.

0 votes

Try this:

Sub getdata()

    Const DB = "\\C:Mydata\database.accdb;"
    Const SQL = "SELECT lot_no FROM [Production Result]"
    
    Dim con As ADODB.Connection
    Set con = New ADODB.Connection
    con.Open ("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" & DB)
    With Sheets("Sheet1").Range("C3")
        .CopyFromRecordset con.Execute(SQL)
    End With
    
End Sub
answered Mar 19 by Kithuzzz
• 35,300 points

Related Questions In Others

0 votes
1 answer

Runtime error 438 while importing data in excel from secured website using VBA

Replace With ieDoc.forms(0) .userType.Value = "1" ...READ MORE

answered Sep 23, 2022 in Others by narikkadan
• 60,820 points
278 views
0 votes
1 answer

How to automatically get a specified range of data from Excel to XML in VBA

Range method works, always identify the sheet ...READ MORE

answered Mar 18 in Others by narikkadan
• 60,820 points
37 views
0 votes
1 answer

Is it possible to get data from a webpage in real-time to an excel file?

The conventional method of obtaining data from ...READ MORE

answered Jan 17 in Others by narikkadan
• 60,820 points
73 views
0 votes
1 answer
0 votes
1 answer

Ms-access vba - read from excel and also update that excel

Here is the code that works without ...READ MORE

answered Dec 28, 2022 in Others by narikkadan
• 60,820 points
247 views
0 votes
1 answer

Merging duplicate cells into a new column with VBA

The requirement to pull the first Open ...READ MORE

answered Mar 23 in Others by narikkadan
• 60,820 points
24 views
0 votes
1 answer

Retrieve epay.info Balance with VBA and Excel

This code should log you in, provided ...READ MORE

answered Sep 5, 2018 in Blockchain by digger
• 26,740 points
577 views
0 votes
1 answer

How to load file to Excel Power query from SFTP site

Currently, I don't think there is a ...READ MORE

answered Dec 3, 2018 in Power BI by Upasana
• 8,620 points
2,563 views
0 votes
1 answer

Select data that meet criteria from a table, adding it to a combobox in userform VBA Excel

Fill Combo Box With Matches Sub GetSourceAcc() ...READ MORE

answered 5 days ago in Others by Kithuzzz
• 35,300 points
24 views
0 votes
1 answer

How do ask ChatGPT with API from Excel macros (vba)?

A few things that will help. Don't ...READ MORE

answered Feb 11 in Others by Kithuzzz
• 35,300 points
634 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP