help with this VBA code (1 Viewer)

dbaker

New member
Local time
Yesterday, 19:20
Joined
Mar 8, 2011
Messages
5
OK, let me preface this by saying that I am very, very new to VBA and realitivly new to ACCESS.

I'm trying to modify a code that will take data out of ACCESS and put it into fields in WORD. I found a code online and I'm trying to edit it to fit my purposes but I'm running into trouble.

Here is the original code:
Code:
Dim appWord As Word.ApplicationDim doc As Word.DocumentDim rst As ADODB.RecordsetDim strSQL As StringDim strReportsTo As StringOn Error Resume NextSet appWord = GetObject(, "Word.application")If Err = 429 Then    Set appWord = New Word.Application    Err = 0End IfWith appWord    Set doc = .Documents(DOC_NAME)    If Err = 0 Then        If MsgBox("Do you want to save the current document " _            & "before updating the data?", vbYesNo) = vbYes Then                .Dialogs(wdDialogFileSaveAs).Show        End If    doc.Close False    End If    On Error GoTo ErrorHandler        Set doc = .Documents.Open(DOC_PATH & DOC_NAME, , True)    Set rst = New ADODB.Recordset        If Not IsNull(Me!ReportsTo) Then        strSQL = "SELECT [FirstName] & "" "" & [LastName] AS Name FROM " _            & "Employees WHERE [EmployeeID]=" & Nz(Me!ReportsTo)        rst.Open strSQL, CurrentProject.Connection, _            adOpenStatic, adLockReadOnly        If Not rst.EOF Then            strReportsTo = Nz(rst.Fields(0).Value)            rst.Close        End If    End If    With doc        .FormFields("fldSalutation").Result = Nz(Me!TitleOfCourtesy)        .FormFields("fldName").Result = Nz(Me!FirstName & " " & Me!LastName)        .FormFields("fldTitle").Result = Nz(Me!Title)        .FormFields("fldHireDate").Result = Nz(Me!HireDate)        .FormFields("fldApprovedBy").Result = strReportsTo    End With    .Visible = True    .ActivateEnd WithSet rst = NothingSet doc = NothingSet appWord = NothingExit SubErrorHandler:MsgBox Err & Err.Description


Can someone help explain to me in as plain of language as possible what the following part of the code accomplishes?

Code:
If Not IsNull(Me!ReportsTo) Then
        strSQL = "SELECT [FirstName] & "" "" & [LastName] AS Name FROM " _
            & "Employees WHERE [EmployeeID]=" & Nz(Me!ReportsTo)
        rst.Open strSQL, CurrentProject.Connection, _
            adOpenStatic, adLockReadOnly
        If Not rst.EOF Then
            strReportsTo = Nz(rst.Fields(0).Value)
            rst.Close
        End If

I realize you might need more information. If so, please just let me know and I will try to answer whatever I can. I will log back on tommorrow morning (about 16 hours from now). Thanks in advance!
 

pbaldy

Wino Moderator
Staff member
Local time
Yesterday, 17:20
Joined
Aug 30, 2003
Messages
36,134
If the contents of the ReportsTo aren't Null, it opens a recordset using that value as a criteria. If that recordset isn't empty, it assigns the value from it to a variable.
 

WayneRyan

AWF VIP
Local time
Today, 01:20
Joined
Nov 19, 2002
Messages
7,122
dbaker,

It basically gets the first/last name of the person who the person on your form reports to.

You could do this with a DLookUp, If they report to nobody, returns "Nobody":

strReportsTo = Nz(DLookUp("[FirstName] & Chr(32) & [LastName]", "Employees", "[EmployeeID] = '" & Me!ReportsTo & "'"), "Nobody")

hth,
Wayne
 

dbaker

New member
Local time
Yesterday, 19:20
Joined
Mar 8, 2011
Messages
5
thanks for the help guys...I dont really need this feature, could I remove it and still be ok?
 

dbaker

New member
Local time
Yesterday, 19:20
Joined
Mar 8, 2011
Messages
5
I've styared over with new code and to avoid any confusion, I've begun a new thread with the new code and new error I'm getting. If you wish to help, the new thread can be found here in the forum under the title:

"Help with a VBA code (different one than previously)

Thanks!
 

Users who are viewing this thread

Top Bottom