Click here to Skip to main content
15,937,186 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
This is not executing the SQL String:
conn.execute "SELECT * FROM VSOP87-0;

I have the Table in "VSOP87-J2000.accdb" called "VSOP87-0". How do I write the correct SQL string for this configuration.
VB
Dim conn As Object
Dim rs As Object
Set conn = CreateObject("ADODB.Connection")  'New ADODB.Connection
conn.Open "Provider=Microsoft.ACE.OLEDB.12.0;Data source=J:\0-Access\VSOP87-J2000.accdb;"
conn.execute "SELECT * FROM VSOP87-0;"
Set rs = CreateObject("ADODB.Recordset")

i = 0
If rs.EOF = False Then
    Do While Not rs.EOF
        A0(i, 0) = rs("A0mer").Value
        B0(i, 0) = rs("B0mer").Value
        C0(i, 0) = rs("C0mer").Value
        A0(i, 1) = rs("A0ven").Value
        B0(i, 1) = rs("B0ven").Value
        C0(i, 4) = rs("C0ven").Value
        A0(i, 2) = rs("A0ear").Value
        B0(i, 2) = rs("B0ear").Value
        C0(i, 2) = rs("C0ear").Value
        A0(i, 3) = rs("A0mar").Value
        B0(i, 3) = rs("B0mar").Value
        C0(i, 3) = rs("C0mar").Value
        A0(i, 4) = rs("A0jup").Value
        B0(i, 4) = rs("B0jup").Value
        C0(i, 4) = rs("C0jup").Value
        A0(i, 5) = rs("A0sat").Value
        B0(i, 5) = rs("B0sat").Value
        C0(i, 5) = rs("C0sat").Value
        A0(i, 6) = rs("A0ura").Value
        B0(i, 6) = rs("B0ura").Value
        C0(i, 6) = rs("C0ura").Value
        A0(i, 7) = rs("A0nep").Value
        B0(i, 7) = rs("B0nep").Value
        C0(i, 7) = rs("C0nep").Value
        i0 = i
        i = i + 1
        rs.MoveNext
    Loop
End If
conn.Close
rs.Close
Set conn = Nothing
Set rs = Nothing


What I have tried:

I have tried:
VB
Dim cn As ADODB.Connection
Dim rs As ADODB.Recordset

This gives an error.

Not sure how the .value on the rs("A0mer") works for all.
Posted
Updated 11-Jan-23 3:01am
v2

There are several issues with your code, the connection string does not seem to be one of them.

1. You are opening a connection to your database and attempting execute some SQL - but you have not defined where the results should go

2. You are creating an empty Recordset and not populating it

3. The table name has an invalid character in the name, the hyphen. So you must surround that name with square brackets in order to get the SQL to work (it would have helped if you had shared details of the error(s) that were reported)

Try this instead
VB
Dim conn As Object, rs As Object
Set conn = CreateObject("ADODB.Connection")
conn.Open "Provider=Microsoft.ACE.OLEDB.12.0;Data source=c:\dev\0-Access\VSOP87-J2000.accdb;"

Set rs = CreateObject("ADODB.Recordset")
rs.Open "SELECT * FROM [VSOP87-0];", conn
4. Your next problem is at the other end of your code - you are attempting to close the Recordset after closing the connection. The latter will effectively close the recordset as well (once you have done the steps above to associate the recordset with the connection). it should be
VB
rs.Close
conn.Close
A general rule of thumb is to close things in the reverse order that you opened them e.g.
A.Open
B.Open
C.Open
.
.
.
C.Close
B.Close
A.Close
As to the rest of it
Quote:
This gives an error.
Give us a clue what the error is, or what version of VB you are using, and we might be able to help with that - although see the link in Solution 1
Quote:
Not sure how the .value on the rs("A0mer") works for all.
Not clear what you mean by this. In your code it is not "working" because that line of code is never executed because rs.EOF is always true in your version
 
Share this answer
 
You can refer the following to know the correct format of connection string: Connection String[^]!

For Access DB, following is the format:
Standard security
Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\myFolder\myAccessFile.accdb;Persist Security Info=False;

With database password (This is the connection string to use when you have an Access 2007 - 2013 database protected with a password using the "Set Database Password" function in Access):
Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\myFolder\myAccessFile.accdb;Jet OLEDB:Database Password=MyDbPassword;

Your case, seems should be:
Provider=Microsoft.ACE.OLEDB.12.0;Data Source=J:\0-Access\VSOP87-J2000.accdb;Persist Security Info=False;

Further info regarding the data communication can be read here: Accessing data with ADO.NET[^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900