Re: Using VBA to create a Table in Access database
- From: "Brian Wilson" <bwilson@xxxxxxxxxx>
- Date: Thu, 6 Apr 2006 22:24:10 +0100
Hey Brian, i've got a prob, access detected an error in this line:
"Set rst = qdf.OpenRecordset(dbOpenForwardOnly)"
before this statement is qdf.sql= sqlstring
sql string is my entire sql statement as i've allowed users earleir to
enter
in data which forms the WHERE clause of the sql statement. thus after
gathering their data, i declared qdf.sql = sqlstring followed by
"Set rst = qdf.OpenRecordset(dbOpenForwardOnly)"
and i got that error. so i removed my sqlstring and tested it with a
simple
ready built pass-through query and i got an error on ".Range("A2").
CopyFromRecordset rst"
But the funny thing is that before all these errors, i managed to export a
file with the intact records. however all the field names were missing,
only
the data was exported not the field names. then when i tried to re-run
again,
the above happened. hopefully u can shed some light on these probs, but at
least i'm getting nearer. thanks for all your help, i'll be leaving office
so
i'll catch u tmr.
Thanks again
Eric
The fact that the field names are not showing is to be expected.
CopyFromRecordset just copies the data. If you need to see the field names,
then you can simply loop through the fields, writing each field name in a
new cell:
With xlSheet
.Name = "Export"
.UsedRange.ClearContents
lngMax = rst.Fields.Count
For lngCount = lngMax To 1 Step -1
.Cells(1, lngCount).Value = rst.Fields(lngCount - 1).Name
Next lngCount
.Range("A2").CopyFromRecordset rst
End With
If you want to modify the code so that you pass in a simple SQL string, you
could use code like that shown below to redefine the pass-through query:
strConnect = "ODBC;" & _
"Driver={SQL Server};" & _
"Server=MyServer;" & _
"Database=" & strDatabase & ";" & _
"Trusted_Connection=yes;"
strSQL = "SELECT ID, FieldOne, FieldTwo FROM " & _
"MyTable WHERE ID<10;"
Set dbs = CurrentDb
Set qdf = dbs.QueryDefs("qryMyPassThrough")
qdf.Connect = strConnect
qdf.SQL = strSQL
As to the other code failing, I can't think whay that might be. I know
there were bugs in previous versions of Office where null values in the
recordset could cause trouble. What version of Excel and Access are you
using?
.
- Follow-Ups:
- Re: Using VBA to create a Table in Access database
- From: Cire via AccessMonster.com
- Re: Using VBA to create a Table in Access database
- From: Cire via AccessMonster.com
- Re: Using VBA to create a Table in Access database
- References:
- Using VBA to create a Table in Access database
- From: Cire via AccessMonster.com
- Re: Using VBA to create a Table in Access database
- From: Duncan Bachen
- Re: Using VBA to create a Table in Access database
- From: Cire via AccessMonster.com
- Re: Using VBA to create a Table in Access database
- From: Cire via AccessMonster.com
- Re: Using VBA to create a Table in Access database
- From: Brian Wilson
- Re: Using VBA to create a Table in Access database
- From: Cire via AccessMonster.com
- Using VBA to create a Table in Access database
- Prev by Date: Re: Booking duplicates
- Next by Date: Access 2003 security warning
- Previous by thread: Re: Using VBA to create a Table in Access database
- Next by thread: Re: Using VBA to create a Table in Access database
- Index(es):
Relevant Pages
|