phill952000
02-03-2009, 03:25 AM
Hi,
i have a situation where i have some form fields in a word document and the information that is inserted into the form fields i want transfered into an existing database and the information to go into the correct fields in the database.
this is what i have so far and i will highlight the line of code that is throwing an error at runtime;
Sub Transfer()
'Transfer new shipping company record to
'Shippers table in Northwind database.
Dim connect As ADODB.Connection
Dim strConnection As String
Dim strexc As String
Dim strPath As String
Dim doc As Word.Document
Dim strCompany As String
Dim strContact As String
Dim strJobTitle As String
Dim strWorkNumber As String
Dim strMobile As String
Dim strFaxNumber As String
Dim strEmail As String
Dim strWebsite As String
Dim strDirectLine As String
Dim strHomeNumber As String
Dim strAddress1 As String
Dim strAddress2 As String
Dim strAddress3 As String
Dim strTown As String
Dim strCounty As String
Dim strPostalCode As String
Dim strSupply As String
Dim strPhoneSpeedDialNo As String
Dim strFaxSpeedDialNo As String
Dim bytContinue As Byte
Dim lngSuccess As Long
Set doc = ThisDocument
'On Error GoTo ErrHandler
strCompany = ActiveDocument.FormFields("Company").Result
strContact = ActiveDocument.FormFields("Contact").Result
strJobTitle = ActiveDocument.FormFields("JobTitle").Result
strWorkNumber = ActiveDocument.FormFields("WorkNumber").Result
strMobile = ActiveDocument.FormFields("Mobile").Result
strFaxNumber = ActiveDocument.FormFields("FaxNumber").Result
strEmail = ActiveDocument.FormFields("Email").Result
strWebsite = ActiveDocument.FormFields("Website").Result
strDirectLine = ActiveDocument.FormFields("DirectLine").Result
strHomeNumber = ActiveDocument.FormFields("HomeNumber").Result
strAddress1 = ActiveDocument.FormFields("Address1").Result
strAddress2 = ActiveDocument.FormFields("Address2").Result
strAddress3 = ActiveDocument.FormFields("Address3").Result
strTown = ActiveDocument.FormFields("Town").Result
strCounty = ActiveDocument.FormFields("County").Result
strPostalCode = ActiveDocument.FormFields("PostalCode").Result
strSupply = ActiveDocument.FormFields("Supply").Result
strPhoneSpeedDialNo = ActiveDocument.FormFields("PhoneSpeedDialNo").Result
strFaxSpeedDialNo = ActiveDocument.FormFields("FaxSpeedDialNo").Result
'Confirm new record.
bytContinue = MsgBox("Do you want to insert this record?", vbYesNo, "Add Record")
Debug.Print bytContinue
'Process input values.
If bytContinue = vbYes Then
strexc = "insert into MGM Address Book " _
& "(Company, Contact, JobTitle, Work Number, Mobile, Fax Number, E-mail, Website, " _
& "Direct Line, Home Number, Address1, Address2, Address3, Town, County, PostalCode, " _
& "Supply, Phone Speed Dial No, Fax Speed Dial No)" _
& "VALUES (" _
& strCompany & ", " _
& strContact & ", " _
& strJobTitle & ", " _
& strWorkNumber & ", " _
& strMobile & ", " _
& strFaxNumber & ", " _
& strEmail & ", " _
& strWebsite & ", " _
& strDirectLine & ", " _
& strHomeNumber & ", " _
& strAddress1 & ", " _
& strAddress2 & ", " _
& strAddress3 & ", " _
& strTown & ", " _
& strCounty & ", " _
& strPostalCode & ", " _
& strSupply & ", " _
& strPhoneSpeedDialNo & ", " _
& strFaxSpeedDialNo & ")"
Debug.Print strexc
'Substitute path and connection string with DSN if available.
strPath = "C:\Documents and Settings\Paul.MGMPLC\My Documents\word to access transfer\MGM ADDRESS BOOK.mdb"
strConnection = "Provider=Microsoft.Jet.OLEDB.4.0;" & "Data Source = " & strPath
Debug.Print strConnection
Set connect = New ADODB.Connection
connect.Open strConnection
connect.Execute strexc, lngSuccess "this line is throwing an error"
connect.Close
MsgBox "You inserted " & lngSuccess & " record", vbOKOnly, "Error Added"
End If
Set doc = Nothing
Set connect = Nothing
Exit Sub
'ErrHandler:
'MsgBox Err.Number & ": " & Err.description, _
'vbOKOnly, "Error"
'On Error GoTo 0
'On Error Resume Next
'cnn.Close
'Set doc = Nothing
'Set cnn = Nothing
End Sub
i have been over this code a few times and cannot devise what is causing this error.
any help is much appriciated
regards
phill952000
EDIT: Tommy Added VBA code tags.
i have a situation where i have some form fields in a word document and the information that is inserted into the form fields i want transfered into an existing database and the information to go into the correct fields in the database.
this is what i have so far and i will highlight the line of code that is throwing an error at runtime;
Sub Transfer()
'Transfer new shipping company record to
'Shippers table in Northwind database.
Dim connect As ADODB.Connection
Dim strConnection As String
Dim strexc As String
Dim strPath As String
Dim doc As Word.Document
Dim strCompany As String
Dim strContact As String
Dim strJobTitle As String
Dim strWorkNumber As String
Dim strMobile As String
Dim strFaxNumber As String
Dim strEmail As String
Dim strWebsite As String
Dim strDirectLine As String
Dim strHomeNumber As String
Dim strAddress1 As String
Dim strAddress2 As String
Dim strAddress3 As String
Dim strTown As String
Dim strCounty As String
Dim strPostalCode As String
Dim strSupply As String
Dim strPhoneSpeedDialNo As String
Dim strFaxSpeedDialNo As String
Dim bytContinue As Byte
Dim lngSuccess As Long
Set doc = ThisDocument
'On Error GoTo ErrHandler
strCompany = ActiveDocument.FormFields("Company").Result
strContact = ActiveDocument.FormFields("Contact").Result
strJobTitle = ActiveDocument.FormFields("JobTitle").Result
strWorkNumber = ActiveDocument.FormFields("WorkNumber").Result
strMobile = ActiveDocument.FormFields("Mobile").Result
strFaxNumber = ActiveDocument.FormFields("FaxNumber").Result
strEmail = ActiveDocument.FormFields("Email").Result
strWebsite = ActiveDocument.FormFields("Website").Result
strDirectLine = ActiveDocument.FormFields("DirectLine").Result
strHomeNumber = ActiveDocument.FormFields("HomeNumber").Result
strAddress1 = ActiveDocument.FormFields("Address1").Result
strAddress2 = ActiveDocument.FormFields("Address2").Result
strAddress3 = ActiveDocument.FormFields("Address3").Result
strTown = ActiveDocument.FormFields("Town").Result
strCounty = ActiveDocument.FormFields("County").Result
strPostalCode = ActiveDocument.FormFields("PostalCode").Result
strSupply = ActiveDocument.FormFields("Supply").Result
strPhoneSpeedDialNo = ActiveDocument.FormFields("PhoneSpeedDialNo").Result
strFaxSpeedDialNo = ActiveDocument.FormFields("FaxSpeedDialNo").Result
'Confirm new record.
bytContinue = MsgBox("Do you want to insert this record?", vbYesNo, "Add Record")
Debug.Print bytContinue
'Process input values.
If bytContinue = vbYes Then
strexc = "insert into MGM Address Book " _
& "(Company, Contact, JobTitle, Work Number, Mobile, Fax Number, E-mail, Website, " _
& "Direct Line, Home Number, Address1, Address2, Address3, Town, County, PostalCode, " _
& "Supply, Phone Speed Dial No, Fax Speed Dial No)" _
& "VALUES (" _
& strCompany & ", " _
& strContact & ", " _
& strJobTitle & ", " _
& strWorkNumber & ", " _
& strMobile & ", " _
& strFaxNumber & ", " _
& strEmail & ", " _
& strWebsite & ", " _
& strDirectLine & ", " _
& strHomeNumber & ", " _
& strAddress1 & ", " _
& strAddress2 & ", " _
& strAddress3 & ", " _
& strTown & ", " _
& strCounty & ", " _
& strPostalCode & ", " _
& strSupply & ", " _
& strPhoneSpeedDialNo & ", " _
& strFaxSpeedDialNo & ")"
Debug.Print strexc
'Substitute path and connection string with DSN if available.
strPath = "C:\Documents and Settings\Paul.MGMPLC\My Documents\word to access transfer\MGM ADDRESS BOOK.mdb"
strConnection = "Provider=Microsoft.Jet.OLEDB.4.0;" & "Data Source = " & strPath
Debug.Print strConnection
Set connect = New ADODB.Connection
connect.Open strConnection
connect.Execute strexc, lngSuccess "this line is throwing an error"
connect.Close
MsgBox "You inserted " & lngSuccess & " record", vbOKOnly, "Error Added"
End If
Set doc = Nothing
Set connect = Nothing
Exit Sub
'ErrHandler:
'MsgBox Err.Number & ": " & Err.description, _
'vbOKOnly, "Error"
'On Error GoTo 0
'On Error Resume Next
'cnn.Close
'Set doc = Nothing
'Set cnn = Nothing
End Sub
i have been over this code a few times and cannot devise what is causing this error.
any help is much appriciated
regards
phill952000
EDIT: Tommy Added VBA code tags.