G
Greg Maxey
I found some code posted by Doug Robbins for transferring data from a
Word formfield to an Access data base. I incorporated that code into a
macro for collecting the results for Word Forms. I have a few
questions:
When if first tried to run Doug's code I got an error on the line:
Dim vConnection As New ADODB.Connection
of them that I thought were related to Access or Objects to get the
code to run and then remove some a block at a time until I narrowed it
down to the one I needed.
How do you know or how can you determine what references are required
to run your code?
Doug's code opens and writes to an existing database. I would prefer
creating a new database and defining the fields in my macro. I think
(I don't know) that if I can figure out how to "create a new" database
vice "open" and existing database that I might be able to define the
structure.
I realize this question might be better suited for an Access group, but
does anyone reading know how to create a new database and table from
VBA in Word.
Doug's code uses "Provider=Microsoft.Jet.OLEDB.4.0;" Doug, and rocket
scientist excluded, how would anyone attempting to write code with
formal training know to use something like that? Is that the way or
one of many ways? If there are others perhaps more straigtforward I
would appreciate seeing a few examples.
Regulars here know that I have a website and I think that showing a
method for collecting data from Word formfields into an Access database
would be helpful to other users. If it exists out there as a FAQ or
website I dont' know about it and spent the better part of a day
scatching together what I have. I certainly would like hear your
comments and suggestions for improvement.
Sub TallyData4()
Dim oPath As String
Dim FileArray() As String
Dim oFileName As String
Dim i As Long
'Requires reference to MS ActiveX Data Objects 2.8 Library
Dim vConnection As New ADODB.Connection
Dim vRecordSet As New ADODB.Recordset
Dim myDoc As Word.Document
oPath = GetPathToUse
If oPath = "" Then
MsgBox "A folder was not selected"
Exit Sub
End If
'Identify files names
oFileName = Dir$(oPath & "*.doc")
ReDim FileArray(1 To 1000) 'A number larger the expected number of
replies
'Add file name to the array
Do While oFileName <> ""
i = i + 1
FileArray(i) = oFileName
'Get the next file name
oFileName = Dir$
Loop
'Resize and preserve the array
ReDim Preserve FileArray(1 To i)
Application.ScreenUpdating = False
'Provide connection string for data using Jet Provider for Access
database
vConnection.ConnectionString = "data source=C:\TestDataBase.mdb;" & _
"Provider=Microsoft.Jet.OLEDB.4.0;"
vConnection.Open
vRecordSet.Open "MyTable", vConnection, adOpenKeyset, adLockOptimistic
'Retrieve the data
vConnection.Execute "DELETE * FROM MyTable"
For i = 1 To UBound(FileArray)
Set myDoc = Documents.Open(FileName:=oPath & FileArray(i), _
Visible:=False)
vRecordSet.AddNew
With myDoc
If .FormFields("Text1").Result <> "" Then _
vRecordSet!Name = .FormFields("Text1").Result
If .FormFields("Text2").Result <> "" Then _
vRecordSet("Favorite Food") = .FormFields("Text2").Result
If .FormFields("Text3").Result <> "" Then _
vRecordSet("Favorite Color") = .FormFields("Text3").Result
.Close
End With
Next i
vRecordSet.Update
vRecordSet.Close
vConnection.Close
Set vRecordSet = Nothing
Set vConnection = Nothing
Application.ScreenUpdating = True
End Sub
Word formfield to an Access data base. I incorporated that code into a
macro for collecting the results for Word Forms. I have a few
questions:
When if first tried to run Doug's code I got an error on the line:
Dim vConnection As New ADODB.Connection
reference. From there it was hunt and peck. I had to just load allFrom past experience I figured that this was due to a missing
of them that I thought were related to Access or Objects to get the
code to run and then remove some a block at a time until I narrowed it
down to the one I needed.
How do you know or how can you determine what references are required
to run your code?
Doug's code opens and writes to an existing database. I would prefer
creating a new database and defining the fields in my macro. I think
(I don't know) that if I can figure out how to "create a new" database
vice "open" and existing database that I might be able to define the
structure.
I realize this question might be better suited for an Access group, but
does anyone reading know how to create a new database and table from
VBA in Word.
Doug's code uses "Provider=Microsoft.Jet.OLEDB.4.0;" Doug, and rocket
scientist excluded, how would anyone attempting to write code with
formal training know to use something like that? Is that the way or
one of many ways? If there are others perhaps more straigtforward I
would appreciate seeing a few examples.
Regulars here know that I have a website and I think that showing a
method for collecting data from Word formfields into an Access database
would be helpful to other users. If it exists out there as a FAQ or
website I dont' know about it and spent the better part of a day
scatching together what I have. I certainly would like hear your
comments and suggestions for improvement.
Sub TallyData4()
Dim oPath As String
Dim FileArray() As String
Dim oFileName As String
Dim i As Long
'Requires reference to MS ActiveX Data Objects 2.8 Library
Dim vConnection As New ADODB.Connection
Dim vRecordSet As New ADODB.Recordset
Dim myDoc As Word.Document
oPath = GetPathToUse
If oPath = "" Then
MsgBox "A folder was not selected"
Exit Sub
End If
'Identify files names
oFileName = Dir$(oPath & "*.doc")
ReDim FileArray(1 To 1000) 'A number larger the expected number of
replies
'Add file name to the array
Do While oFileName <> ""
i = i + 1
FileArray(i) = oFileName
'Get the next file name
oFileName = Dir$
Loop
'Resize and preserve the array
ReDim Preserve FileArray(1 To i)
Application.ScreenUpdating = False
'Provide connection string for data using Jet Provider for Access
database
vConnection.ConnectionString = "data source=C:\TestDataBase.mdb;" & _
"Provider=Microsoft.Jet.OLEDB.4.0;"
vConnection.Open
vRecordSet.Open "MyTable", vConnection, adOpenKeyset, adLockOptimistic
'Retrieve the data
vConnection.Execute "DELETE * FROM MyTable"
For i = 1 To UBound(FileArray)
Set myDoc = Documents.Open(FileName:=oPath & FileArray(i), _
Visible:=False)
vRecordSet.AddNew
With myDoc
If .FormFields("Text1").Result <> "" Then _
vRecordSet!Name = .FormFields("Text1").Result
If .FormFields("Text2").Result <> "" Then _
vRecordSet("Favorite Food") = .FormFields("Text2").Result
If .FormFields("Text3").Result <> "" Then _
vRecordSet("Favorite Color") = .FormFields("Text3").Result
.Close
End With
Next i
vRecordSet.Update
vRecordSet.Close
vConnection.Close
Set vRecordSet = Nothing
Set vConnection = Nothing
Application.ScreenUpdating = True
End Sub