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, 2023 in Others by narikkadan
• 63,420 points
427 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, 2023 by Kithuzzz
• 38,010 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
• 63,420 points
669 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, 2023 in Others by narikkadan
• 63,420 points
511 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, 2023 in Others by narikkadan
• 63,420 points
264 views
0 votes
1 answer

Excel VBA search based on cell values into folders and sub-folders to get the file path and data

This will create a listing of all ...READ MORE

answered Jan 19, 2023 in Others by narikkadan
• 63,420 points
1,548 views
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
• 63,420 points
1,139 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, 2023 in Others by narikkadan
• 63,420 points
170 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
876 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
3,183 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 Mar 26, 2023 in Others by Kithuzzz
• 38,010 points
432 views
0 votes
1 answer

Excel: importing data from another Excel file using VBA

Refer to the file variables folderName and ...READ MORE

answered Apr 7, 2023 in Others by Kithuzzz
• 38,010 points
440 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