Results 1 to 6 of 6
  1. #1
    alpinegroove is offline Competent Performer
    Windows XP Access 2002
    Join Date
    Jan 2011
    Posts
    114

    .GoTo and .TypeText - Stops When Field is Empty

    I am using VBA code to insert text from Access fields into bookmarked locations in Word.

    However, the code aborts when it reaches an empty field. The document is created and data is inserted, but it does not insert data in bookmarks listed in the code below the empty field.

    For example, if [StreetAddress] is empty, [State] and [ZipCode] will not be inserted even if those fields are not empty.



    Is there a way to tell the code to continue even if a field is empty?

    Thanks

    Code:
    Option Compare Database
    
    Private Sub GenerateContract_Click()
    
       ' Check for empty fields and unsaved record.
       If IsNull(FirstName) Then
         MsgBox "First name cannot be empty"
         Me.FirstName.SetFocus
         Exit Sub
       End If
       If IsNull(LastName) Then
         MsgBox "Last name cannot be empty"
         Me.LastName.SetFocus
         Exit Sub
       End If
       If IsNull(StreetAddress) Then
         MsgBox "Street address cannot be empty"
         Me.StreetAddress.SetFocus
         Exit Sub
       End If
       
       If Me.Dirty Then
         If MsgBox("Record has not been saved. " & Chr(13) & _
             "Do you want to save it?", vbInformation + vbOKCancel) = vbOK Then
           DoCmd.RunCommand acCmdSaveRecord
         Else
           Exit Sub
         End If
       End If
            
       ' Create a Word document from template.
       Dim WordApp As Word.Application
       Dim strTemplateLocation As String
      
       ' Specify location of template
       strTemplateLocation = "H:\Instructor Contract Template.doc"
        
        
       On Error Resume Next
       Set WordApp = GetObject(, "Word.Application")
       If Err.Number <> 0 Then
         Set WordApp = CreateObject("Word.Application")
       End If
       On Error GoTo ErrHandler
       
       
       WordApp.Visible = True
       WordApp.WindowState = wdWindowStateMaximize
       WordApp.Documents.Add Template:=strTemplateLocation, NewTemplate:=False
        
       ' Replace each bookmark with field contents.
       With WordApp.Selection
       
         .GoTo what:=wdGoToBookmark, Name:="Name"
         .TypeText Trim(Me.[FirstName] & " " & Me.[LastName])
       
         .GoTo what:=wdGoToBookmark, Name:="StreetAddress"
         .TypeText Me.[StreetAddress]
        
         .GoTo what:=wdGoToBookmark, Name:="City"
         .TypeText Me.[City]
      
         .GoTo what:=wdGoToBookmark, Name:="State"
         .TypeText Me.[State]
        
         .GoTo what:=wdGoToBookmark, Name:="ZipCode"
         .TypeText Me.[ZipCode]
             
         .GoTo what:=wdGoToBookmark, Name:="Name2"
         .TypeText Trim(Me.[FirstName] & " " & Me.[LastName])
         
         .GoTo what:=wdGoToBookmark, Name:="CourseTitle"
         .TypeText Me.[CourseTitle]
         
         .GoTo what:=wdGoToBookmark, Name:="Section"
         .TypeText Me.[SectionNumber]
         
         .GoTo what:=wdGoToBookmark, Name:="CourseDT"
         .TypeText Me.[CourseD/T]
         
         .GoTo what:=wdGoToBookmark, Name:="CourseRoom"
         .TypeText Me.[CourseRoom]
         
         If IsNull(Me.[CourseFinal D/T/R]) Then
         .GoTo what:=wdGoToBookmark, Name:="CourseFinalDTR"
         .TypeText Text:="N/A"
         Else
         .GoTo what:=wdGoToBookmark, Name:="CourseFinalDTR"
         .TypeText Me.[CourseFinal D/T/R]
         End If
                  
         .GoTo what:=wdGoToBookmark, Name:="CourseFinalDate"
         .TypeText Me.[CourseFinal Date]
         
         .GoTo what:=wdGoToBookmark, Name:="CourseDisc1DT"
         .TypeText Me.[CourseDisc1D/T]
              
         .GoTo what:=wdGoToBookmark, Name:="CourseDisc1Rm"
         .TypeText Me.[CourseDisc1Rm]
         
         .GoTo what:=wdGoToBookmark, Name:="CourseDisc2DT"
         .TypeText Me.[CourseDisc2D/T]
              
         .GoTo what:=wdGoToBookmark, Name:="CourseDisc2Rm"
         .TypeText Me.[CourseDisc2Rm]
         
         .GoTo what:=wdGoToBookmark, Name:="CourseDisc3DT"
         .TypeText Me.[CourseDisc3D/T]
              
         .GoTo what:=wdGoToBookmark, Name:="CourseDisc3Rm"
         .TypeText Me.[CourseDisc3Rm]
         
         .GoTo what:=wdGoToBookmark, Name:="CourseDisc4DT"
         .TypeText Me.[CourseDisc4D/T]
              
         .GoTo what:=wdGoToBookmark, Name:="CourseDisc4Rm"
         .TypeText Me.[CourseDisc4Rm]
         
         .GoTo what:=wdGoToBookmark, Name:="InstructorComp"
         .TypeText FormatCurrency(Me.[Instructor])
         
         .GoTo what:=wdGoToBookmark, Name:="TAComp"
         .TypeText FormatCurrency(Me.[TA])
         
         .GoTo what:=wdGoToBookmark, Name:="ReaderComp"
         .TypeText FormatCurrency(Me.[Reader])
              
       End With
        
       DoEvents
       WordApp.Activate
        
       Set WordApp = Nothing
       Exit Sub
    
    ErrHandler:
    Set WordApp = Nothing
    
    End Sub

  2. #2
    June7's Avatar
    June7 is online now VIP
    Windows XP Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,928
    Assume the emptiness is a Null. Handle null with Nz function to process an alternate value.

    .TypeText Trim(Nz(Me.[FirstName], "") & " " & Nz(Me.[LastName], ""))
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  3. #3
    alpinegroove is offline Competent Performer
    Windows XP Access 2002
    Join Date
    Jan 2011
    Posts
    114
    Thank you.

  4. #4
    alpinegroove is offline Competent Performer
    Windows XP Access 2002
    Join Date
    Jan 2011
    Posts
    114
    This worked well, but I am stuck when trying to use FormatCurrency in addition.

    This is working fine:
    .TypeText Nz(Me.[CourseFinalDate], "N/A")

    This is not working:
    .TypeText Nz(FormatCurrency(Me.[TA]), "N/A")

    How do I incorporate formatting into .TypeText Nz?

  5. #5
    June7's Avatar
    June7 is online now VIP
    Windows XP Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,928
    Nz not applicable. FormatCurrency function should handle the Null and return an empty string (and the Nz will never see Null). If you want something other than empty string to process, need more complex expression.

    IIf(IsNull(Me.[TA]),"N/A",FormatCurrency(Me.[TA]))
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  6. #6
    alpinegroove is offline Competent Performer
    Windows XP Access 2002
    Join Date
    Jan 2011
    Posts
    114
    Perfect, thank you!

Please reply to this thread with any new information or opinions.

Similar Threads

  1. VBA TypeText - Retain Formatting?
    By alpinegroove in forum Programming
    Replies: 2
    Last Post: 01-02-2012, 04:16 PM
  2. Replies: 4
    Last Post: 11-20-2011, 01:08 PM
  3. Date Field empty if statement
    By dubsdj in forum Programming
    Replies: 4
    Last Post: 03-06-2011, 04:02 PM
  4. Delete Field if Empty?
    By bpowers2010 in forum Reports
    Replies: 1
    Last Post: 07-10-2010, 12:04 AM
  5. empty field
    By amber mara in forum Access
    Replies: 2
    Last Post: 05-05-2010, 01:46 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Other Forums: Microsoft Office Forums