Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
DiscussionsAccessExcelInfoPathOutlookPowerPointPublisherWord
DirectoryUser Groups
Related Topics
Outlook ExpressInternet ExplorerWindowsMS Server ProductsMore Topics ...

MS Office Forum / Word / Programming / August 2006

Tip: Looking for answers? Try searching our database.

assigning variable to DB field value

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
muyBN - 29 Aug 2006 18:15 GMT
In the following code (brackets [] replace actual path name, etc.), what is
the syntax for assigning the value of the database field to a variable?

And a few more questions:

What would be the equivalent code for doing the same but using a recordset
just to get the data field value then assign it to a variable? In other
words, this would just retrieve the field's value and not make the document
into a merge document, correct?

If I didn't want to use the .odc shown below, what would be the correct
syntax to just reference an Access database named DB.mdb? (I'm asking because
I recorded this from a macro; and I'd like to know all the options so I can
better understand the syntax and principles behind the code.)

   ActiveDocument.MailMerge.OpenDataSource Name:="[path]\[datasource].odc" _
       , ConfirmConversions:=False, ReadOnly:=False, LinkToSource:=True,
AddToRecentFiles:=False, PasswordDocument:="", PasswordTemplate:="", _
       WritePasswordDocument:="", WritePasswordTemplate:="", Revert:=False,
Format:=wdOpenFormatAuto, Connection:= _
       "Provider=MSDASQL.1;Persist Security Info=False;User ID=b;Extended
Properties=""DBQ=[path]\[database].mdb;DefaultDir=[path];" & _
       "Driver={Microsoft Access Driver (*.mdb)};DriverId=25;FIL=MS
Access;FILEDSN=[path]\[DSN].dsn;MaxBufferSize=2048;MaxScanRows=8;PageTi" _
       , SQLStatement:="SELECT [field] FROM `[query]`", SQLStatement1:="",
SubType:=wdMergeSubTypeOther
   [variable]=???

Signature

Thanks in advance, Bryan

muyBN - 30 Aug 2006 07:23 GMT
Hey, all the questions around mine have been answered! What's wrong with mine?
Signature

Bryan

> In the following code (brackets [] replace actual path name, etc.), what is
> the syntax for assigning the value of the database field to a variable?
[quoted text clipped - 23 lines]
> SubType:=wdMergeSubTypeOther
>     [variable]=???
muyBN - 30 Aug 2006 08:39 GMT
Don't need you! Figured it out after hours of trial and error:

Dim strSQL As String, [variable] As String
Dim objConn As New ADODB.Connection, rsRecSet As New ADODB.Recordset

   strSQL = "SELECT [field] FROM [query];"
   Set objConn = CreateObject("adodb.connection")
   objConn.ConnectionString = "data source=[path]\[file].mdb;
Provider=Microsoft.Jet.OLEDB.4.0;"
   objConn.Open
   Set rsRecSet = CreateObject("adodb.recordset")
   rsRecSet.Open strSQL, objConn
   [variable] = rsRecSet("[field]")
   objConn.Close
   rsRecSet.Close

Signature

Bryan

> Hey, all the questions around mine have been answered! What's wrong with mine?
>
[quoted text clipped - 25 lines]
> > SubType:=wdMergeSubTypeOther
> >     [variable]=???
muyBN - 30 Aug 2006 10:41 GMT
OK, I will admit, I did what I needed with the recordset, but I still need
help on how to set a variable equal to the value of a merge field; something
like:

variable = activedocument.mailmerge.fields[WhatNext?]"""[field]"""
Signature

Bryan

> In the following code (brackets [] replace actual path name, etc.), what is
> the syntax for assigning the value of the database field to a variable?
[quoted text clipped - 23 lines]
> SubType:=wdMergeSubTypeOther
>     [variable]=???
Peter Jamieson - 30 Aug 2006 15:10 GMT
variable = Activedocument.MailMerge.DataSource.DataFields("the name of the
field in the data source")

Peter Jamieson
> OK, I will admit, I did what I needed with the recordset, but I still need
> help on how to set a variable equal to the value of a merge field;
[quoted text clipped - 40 lines]
>> SubType:=wdMergeSubTypeOther
>>     [variable]=???
muyBN - 30 Aug 2006 21:25 GMT
Ooh, missed it by that much! Then would you believe, by that much?

Thanks, Peter! Your answer was exactly what I needed.
Signature

Bryan

> variable = Activedocument.MailMerge.DataSource.DataFields("the name of the
> field in the data source")
[quoted text clipped - 44 lines]
> >> SubType:=wdMergeSubTypeOther
> >>     [variable]=???

Rate this thread:






 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.