The code below works to dreate a dynamic connection to a database which can be specified at runtime.<br>
However the line which is commented out which, that was to allow me to specify which table to connect to at runtime also, is not working. I get:<br>
Runtime error 3709 The application requested an operation on an object with a reference to a closed or invalid connecttion object.<br>
How do I specify table at runtime<br>
<br>
Dim Db As Connection<br>
Dim dbname As String 'database name<br>
Dim tname As String 'table name<br>
Dim sqlselect As String 'select statement<br>
Dim Rs As Recordset<br>
dbname = "A"<br>
tname = "1"<br>
Set Db = New Connection 'setting database connection<br>
Db.Open "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\F\Databases\Stocks\" & dbname & ".mdb"<br>
<br>
Set Rs = New Recordset 'setting recordset<br>
sqlselect = "select * from " & tname & ", db, adopenstatic, adlockoptimistic"<br>
'rs.Open "select * from " & tname & ", db, adopenstatic, adlockoptimistic" 'not working<br>
Rs.Open "select * from 1", Db, adOpenStatic, adLockOptimistic 'working<br>
<br>
'Rs.ActiveConnection = Db 'this was an alternative type of attempt but the following line<br>
'rs.Source = "select * from " tname 'gives an unexpected end of statement<br>
<br>
Set Text9.DataSource = Rs 'binding control<br>
Text9.DataField = "High"<br>
However the line which is commented out which, that was to allow me to specify which table to connect to at runtime also, is not working. I get:<br>
Runtime error 3709 The application requested an operation on an object with a reference to a closed or invalid connecttion object.<br>
How do I specify table at runtime<br>
<br>
Dim Db As Connection<br>
Dim dbname As String 'database name<br>
Dim tname As String 'table name<br>
Dim sqlselect As String 'select statement<br>
Dim Rs As Recordset<br>
dbname = "A"<br>
tname = "1"<br>
Set Db = New Connection 'setting database connection<br>
Db.Open "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\F\Databases\Stocks\" & dbname & ".mdb"<br>
<br>
Set Rs = New Recordset 'setting recordset<br>
sqlselect = "select * from " & tname & ", db, adopenstatic, adlockoptimistic"<br>
'rs.Open "select * from " & tname & ", db, adopenstatic, adlockoptimistic" 'not working<br>
Rs.Open "select * from 1", Db, adOpenStatic, adLockOptimistic 'working<br>
<br>
'Rs.ActiveConnection = Db 'this was an alternative type of attempt but the following line<br>
'rs.Source = "select * from " tname 'gives an unexpected end of statement<br>
<br>
Set Text9.DataSource = Rs 'binding control<br>
Text9.DataField = "High"<br>