Log in

View Full Version : Solved: connect a word VBA form to access database



hometech
10-24-2012, 07:15 AM
currently working on inserting a form into my MS word application. the user of the application will click on a command button and the form loads and the user then enters his data on the form which is then stored to an access database.
Now when i added the ADODC control to my form, i specified the command type, record source and built the connection string (all from the properties box in the VBE)when i clicked on the textbox to bind it to the required field in the database the there is no way to specify the data source(ADODC), the data field that stores the content of the textbox. Please is there anyway to go about this programatically (VBA) or any other suggestion.
Thanks

hometech
10-29-2012, 10:15 AM
Hello, still working on this, stumbled on this code to write data into database and every thing seems fine only that when i run the code it gives me This error message "Number of query fields and destination value are not the same"
how do i go about this????



Sub insertValuesToDB()
Dim valueRead As String
Dim tyme As String
Application.Selection.Expand wdLine
tyme = Date
valueRead = "This is my comment"
Dim adoConn As ADODB.Connection
Dim adoCmd As ADODB.Command
Set adoConn = New ADODB.Connection

With adoConn
.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\Users\McBajo\Desktop\CPMS\M&CAutomation.mdb"
.Open
End With

Set adoCmd = New ADODB.Command
With adoCmd
.ActiveConnection = adoConn
.CommandText = "INSERT INTO Comments (cmtdate, cmtComment) VALUES ('" & (tyme) & ", " & (valueRead) & "')"
End With

adoCmd.Execute
adoConn.Close
Set adoConn = Nothing
MsgBox "Value was added to your database table."
End Sub


My database has only two fields and i dont seem to understand this message, thanks.

hometech
10-30-2012, 04:38 AM
Figured out where the problem came from, the "Number of query fields and destination value are not the same" occurred as a result of the missing (') wrapping the field name/content



.CommandText = "INSERT INTO Comments (cmtComment, cmtDate) VALUES ('" & valueRead & "', '" & tyme & " ')"



now it inserts into the database just fine

hometech
10-30-2012, 08:59 AM
Hey guys got another problem, i have another section of code that reads data from an access database into a Bookmark in MS Word.

Now the catch here is this, i want to be able to display all the records in my database within the MS Word table that contains the bookmark, when i run the Macro, it reads from the database and overwrite the records in the table field, when i click the Undo button i see all the previous records it already inserted.

i want to be able to display the records of the database line by line eg
10/12/2012 - blah, blah blah...
03/04/2012 - blah, blah, blah...
and so on for the remaining records within the table



Private Sub cmdInsert_Click()
Dim dbs As DAO.Database
Dim rst As DAO.Recordset
Dim i As Integer

Set dbs = OpenDatabase(ActiveDocument.Path & "\M&CAutomation.mdb")
Set rst = dbs.OpenRecordset("Select * FROM Comments;")
Do While Not rst.EOF
Call FillBookmark("" & rst.Fields("cmtDate") & Chr$(45) & rst.Fields("cmtComment") & vbCrLf, "bmComment")
rst.MoveNext
Loop
ActiveDocument.Fields.Update 'Update linked fields
Set rst = Nothing
Set dbs = Nothing
End Sub


Please any idea on how to go about this? you suggestions are highly appreciated:help:help

hometech
10-31-2012, 06:02 AM
found exactly what i needed @

http://www.vb-helper.com/howto_access_to_word_table.html

i guess i asked all the questions and answered it all by myself.....