I have a very simply access database( .mdb format) that contains one table with one column. There is no data in the database.
I am trying to use it to collect the text value of an undetermined number of fields in Word document. Working toward that goal right now I am only trying to figure out how to add a number of columns to the table. I will need a column for each uniquely named
field in the document.
The code below adds one new column with the fixed heading "Column2" to the table. The rest of the iterations of the loop are skipped due to the Error handling.
vConnection.ConnectionString = "data source=D:\Batch\Tally Data Forms\Extracted Data.mdb;" & _
"Provider=Microsoft.Jet.OLEDB.4.0;"
vConnection.Open
On Error Resume Next
For i = 2 To 10
vConnection.Execute "ALTER TABLE MyTable ADD COLUMN Column2 Text;"
Next i
I need to figure out how to make: "ALTER TABLE MyTable ADD COLUMN Column2 Text;" a varialbe value something like
Dim pStr as String
For i = 2 To 10
pStr = "ALTER TABLE MyTable ADD COLUMN Column " & i & "Text;"
vConnection.Execute pStr
Next i
So that I end up with 10 columns "Column 2", "Column 3", etc.
When I tried the technique above I keep getting a syntax error.
My ultimate goal is to create something like this:
Dim oCC as ContentControl
vConnection.ConnectionString = "data source=D:\Batch\Tally Data Forms\Extracted Data.mdb;" & _
"Provider=Microsoft.Jet.OLEDB.4.0;"
vConnection.Open
On Error Resume Next
For Each oCC in ActiveDocument.ContentControls
'Make a column for each titled Content Control
pStr = "ALTER TABLE MyTable ADD COLUMN " & oCC.Title & "Text;"
vConnection.Execute pStr
Next oCC
'Delete previous data
vConnection.Execute "DELETE * FROM MyTable"
For Each oCC in ActiveDocument.ContentControls
vRecordSet(oCC.Title) = oCC.Range.Text
Next oCC
Thanks.
Greg Maxey --- Visit my website at: http://gregmaxey.mvps.org/word\_tips.htm