|
-
Aug 21st, 2008, 11:25 PM
#1
Thread Starter
New Member
[RESOLVED] ODBC call from Word Userform in VBA
Hi, I have a userform I was populating with data via an ADODB call (sql string) to an Access database which is updated overnight each day. This works fine under some conditions, but what I really need is an ODBC call to a server so I can get dynamic, up to date data coming from the server to the userform. Can anyone assist with the code for this one?
I know I'll have to use Hostname, accountpath, username and password to get this happening but I've only ever done ADODB stuff before so I'm completely in the dark.
Any assistance greatly appreciated.
-
Aug 22nd, 2008, 04:24 AM
#2
Re: ODBC call from Word Userform in VBA
check out the database development forum, or google for connection strings
i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next
dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part
come back and mark your original post as resolved if your problem is fixed
pete
-
Aug 22nd, 2008, 09:54 AM
#3
Re: ODBC call from Word Userform in VBA
You don't need to use ODBC, and ideally shouldn't, as OLEDB is newer so is better in several ways. With either, you can/should use ADODB.
You didn't mention what kind of database (Access/SQL Server/...) you want to connect to, but the chances are that the only essential change is the Connection String. You can find examples via the link in my signature.
-
Aug 23rd, 2008, 06:19 AM
#4
Thread Starter
New Member
Re: ODBC call from Word Userform in VBA
OK, thanks...I'll check the link you have provided re connection strings. I believe the server is Universe, which I'm assuming is a unix environment...but I'm not an expert in that field.
Will post as soon as I know more as am not at work right now.
thanks again...
-
Aug 25th, 2008, 06:07 PM
#5
Thread Starter
New Member
Re: ODBC call from Word Userform in VBA
OK, I've checked Connection Strings but can't see anything for Universe, a Unix based server with a Sloaris OS, which is what I'll need to connect to...
Any thoughts?
-
Aug 26th, 2008, 01:44 AM
#6
Thread Starter
New Member
Re: ODBC call from Word Userform in VBA
Anyone able to help??
I need to know what sort of connection strings I can use for VBA to a Unix server and whether I can interrogate the server with an sql statement?
Thanks
-
Aug 26th, 2008, 03:23 AM
#7
Re: ODBC call from Word Userform in VBA
i found this
* OLE DB Provider for UniData and UniVerse
oConn.Open "Provider=Ardent.UniOLEDB;" & _
"Data source=myServer;" & _
"Location=myDatabase;" & _
"User ID=myUsername;" & _
"Password=myPassword"
For more information, see: Ardent Using UniOLEDB 5.1, Informix Using UniOLEDB 5.2
http://puma.clunet.edu/is/ManualsDoc...3-1005-1_0.pdf
google is wonderful
i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next
dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part
come back and mark your original post as resolved if your problem is fixed
pete
-
Aug 27th, 2008, 06:52 PM
#8
Thread Starter
New Member
Re: ODBC call from Word Userform in VBA
Thanks for that connection detail. The issue is now how I go about interrogating the server...I don't think an sql statement is appropriate and there's is very limited info on the web re this.
So if anyone has any ideas on how I get specific data from fields on the server and use it to populate fields on my userform then I would greatly appreciate it.
Thanks very much
-
Aug 28th, 2008, 04:29 AM
#9
Re: ODBC call from Word Userform in VBA
What makes you think SQL statements are not appropriate for it?
I have used many different database systems over the years, and have yet to find one that can be used in a better way.
In case you don't know how to run one, here's an example:
Code:
'Connect to the database
Dim objCn as ADODB.Connection
Set objCn = New ADODB.Connection
objCn.Open "<your connection string here"
'Create a recordset so you can get data
Dim objRS as ADODB.Recordset
Set objRS = New ADODB.Recordset
'Create an SQL statement to specify the data you want
** change this to suit the tables & fields in your database
Dim strSQL as String
strSQL = "SELECT Field1 " _
& "FROM Table1 " _
& "WHERE Field2 = value"
'get the data by opening the recordset, using the connection and SQL
objRS.Open strSQL, objCN, adOpenForwardOnly, adLockReadOnly, adCmdText
'check if there is any data returned
If objRS.EOF Then
MsgBox "There was no data "
Else
'use the data that is in the recordset
Do While Not objRS.EOF
** change this to suit the fields in your SQL statement, and to do what you want
Msgbox objRS("Field1").Value
objRS.MoveNext
Loop
End If
'when finished with the recordset, close it
objRS.Close
Set objRS = Nothing
'when finished with the connection (and all recordsets that used it), close it
objCn.Close
Set objCn = Nothing
If there are parts of this you don't understand, there is a good chance that they are explained by articles in our Database Development FAQs/Tutorials (at the top of the Database Development forum)
-
Aug 28th, 2008, 04:41 AM
#10
Re: ODBC call from Word Userform in VBA
is the server local or on the internet?
i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next
dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part
come back and mark your original post as resolved if your problem is fixed
pete
-
Aug 28th, 2008, 05:31 PM
#11
Thread Starter
New Member
Re: ODBC call from Word Userform in VBA
Right, my mistake...I had assumed, obviously incorrectly, that sql wasn't appropriate in this case. I have used sql statements to interrogate my Access db from a Word userform so now that I know I should be totally fine. Many many thanks for sticking with this thread and the excellent advice.
I can now proceed with putting the final touches to my new document automation system and get it launched for all staff to use.
Cheers,
Peter
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|