Cederbusch
03-13-2012, 12:52 AM
Yesterday I learned how to access our database with VBA, extract the information I want and present it in Excel. :beerchug:
But now to the next challange: I need to do the same query in Word and present the result in a textbox. I´ve searched the Internet but it´s to complicated. I need someone to explain it to me. There is a Swedish saying: "Talk to a farmer as a Farmer".
This is the code I have for Excel. As I´m not able to present the result in Word I have no idea if this works in Word.
Sub GetDataTest()
Dim objMyConn As ADODB.Connection
Dim objMyRecordset As ADODB.Recordset
Set objMyConn = New ADODB.Connection
objMyConn.ConnectionString = "Provider=SQLOLEDB;Data Source=myServer;Initial Catalog=myDB;User ID=myID;Password=myPW;"
objMyConn.Open
Set objMyRecordset = New ADODB.Recordset
objMyRecordset.ActiveConnection = objMyConn
objMyRecordset.Source = "SELECT [First Name] FROM EmployessTable WHERE [Employee]='10000'"
objMyRecordset.Open
' Here´s where I´m lost....
ActiveSheet.Range("A1").CopyFromRecordset objMyRecordset
objMyRecordset.Close
objMyConn.Close
Set objMyConn = Nothing
Set objMyRecordset = Nothing
End Sub
Could someone plz explain to me how to get this working in Word 2007? : pray2:
/Cederbusch
But now to the next challange: I need to do the same query in Word and present the result in a textbox. I´ve searched the Internet but it´s to complicated. I need someone to explain it to me. There is a Swedish saying: "Talk to a farmer as a Farmer".
This is the code I have for Excel. As I´m not able to present the result in Word I have no idea if this works in Word.
Sub GetDataTest()
Dim objMyConn As ADODB.Connection
Dim objMyRecordset As ADODB.Recordset
Set objMyConn = New ADODB.Connection
objMyConn.ConnectionString = "Provider=SQLOLEDB;Data Source=myServer;Initial Catalog=myDB;User ID=myID;Password=myPW;"
objMyConn.Open
Set objMyRecordset = New ADODB.Recordset
objMyRecordset.ActiveConnection = objMyConn
objMyRecordset.Source = "SELECT [First Name] FROM EmployessTable WHERE [Employee]='10000'"
objMyRecordset.Open
' Here´s where I´m lost....
ActiveSheet.Range("A1").CopyFromRecordset objMyRecordset
objMyRecordset.Close
objMyConn.Close
Set objMyConn = Nothing
Set objMyRecordset = Nothing
End Sub
Could someone plz explain to me how to get this working in Word 2007? : pray2:
/Cederbusch