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 / February 2007

Tip: Looking for answers? Try searching our database.

Error Handling DAO Is Null BoundColumn?

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
jkbourland@att.net - 09 Jan 2007 00:11 GMT
Hi,

I am currently working in a Word 2003 VBA User Form.  I have been using
the DAO excel table as my list of employees and data that pulls into
the combo box. (got away from Word Tables)  I know how to trap the
error of Null (error 94 Invalid use of Null) by using a If statement
for every boundcolumn...but I was hoping I could accomplish two things
with out typing as much code.  I placed code with ** asterisks to lead
you to my questions in the code.

cmbAuthorsInitials is where the DAO code gets pulled into.

1. Create an error handler that will have one If stmt and handle the
code for each error that occurs.....or is that not the right way to do
it?

2. Simplify code so it isn't retyped over and over again.  I was hoping
to call a sub routine that has all the Dim and Variable information in
it...so I could call on it when I need it and not type the same code
over and over again.  It would hold all the code listed below.  This
way if the DAO boundcolumn changed for Phone I change it in one
location and the variable that is assigned to it now pulls the right
column into the text field therefore into the document.

The problem I am having is this.  The macro below "Sub MyFormFields"
works fine in the exit event of the cmbAuthorsInititals.  This makes it
so it displays the proper column text based on the cmbAuthorsInitials
choice.   But it doesn't work when I want to use the same variables to
insert a signature line into the document.

i.e.  This is in the cmdOK_Click event.  It wont pull in my variable
names AClosing with out repeating everything again.  Is there s way
around this?
Sub cmdOK_Click()
 MyFormFields
 TheSignature = AClosing & "," & vbCr & vbCr & vbCr & vbCr & AName &
vbCr & ATitle & vbCr & UCase(AInitials) & ":" & TInitials
       If ActiveDocument.Bookmarks.Exists("Signature") = True Then
        Selection.GoTo what:=wdGoToBookmark, Name:="Signature"
        Selection.Text = TheSignature
        ActiveDocument.Bookmarks.Add Range:=Selection.Range,
Name:="Signature"
        'UpdateBookmarkText .Bookmarks("Signature"), TheSignature
   End If

Thank you for your help....Kerri

CODE:
Sub MyFormFields()
'**  Use variables here so I don't have to set back to BoundColumn if
used in cmdOk_Click ect...
   Dim AClosing As String      'Very Truly Yours
   Dim AName As String         'Authors Name
   Dim AInitials As String     'Authors Initials
   Dim TInitials As String     'Typist Inititals
   Dim ATitle As String        'Authors Title
      Dim TheSignature As String  ' all above values inserted.
   Dim ALocation As String     'Authors Location
   Dim APhone As String        'Authors Phone

On Error GoTo ErrorHandler

   cmbAuthorsInitials.BoundColumn = 1 'AIntitials
   cmbAuthorsInitials.Value = cmbAuthorsInitials.Value
   AInitials = cmbAuthorsInitials.Value

   cmbAuthorsInitials.BoundColumn = 2 'Name
   txtAuthorsName.Text = cmbAuthorsInitials.Value
   AName = txtAuthorsName.Text

'**  I know for a fact it will error here.  Not everyone has a title.
   cmbAuthorsInitials.BoundColumn = 4 'Authors Title
   If IsNull(cmbAuthorsInitials.Value) Then
       txtAuthorsTitle.Text = ""
       ATitle = txtAuthorsTitle.Text
   Else
       cmbAuthorsInitials.BoundColumn = 4 'Authors Title
       txtAuthorsTitle.Text = cmbAuthorsInitials.Value
       ATitle = txtAuthorsTitle.Text
   End If

   cmbAuthorsInitials.BoundColumn = 6 'Authors Phone
   txtAuthorsPhone.Text = cmbAuthorsInitials.Value
   APhone = txtAuthorsPhone.Text

   cmbAuthorsInitials.BoundColumn = 7 'Location
   cmbOfficeLocation.Value = cmbAuthorsInitials.Value
   ALocation = cmbOfficeLocation.Value

   cmbAuthorsInitials.BoundColumn = 8 'Closing
   txtAuthorsClosing.Text = cmbAuthorsInitials.Value
   AClosing = txtAuthorsClosing.Text

   cmbAuthorsInitials.BoundColumn = 9 'Typist Initials
   txtTypistInitials.Text = cmbAuthorsInitials.Value
   TInitials = txtTypistInitials.Text

Exit Sub
ErrorHandler:
  If Err.Number = "94" Then
'**   'This is how I can trap the error but I need to know what the
text was at the line of the error to customize it for each line it
happens on.
     If IsNull(cmbAuthorsInitials.Value) Then
     MsgBox Err.Description  'Don't need just to see error
'**   I want the first code to appear here on the line it caused the
error i.e. txtAuthorsPhone.text  = ""
     Resume Next
     End If
  End If
 
End Sub
Perry - 28 Jan 2007 13:44 GMT
Hmm, huge text in yr msg.
Ok, here we go.

First: you don't need to shift the BoundColumn property in order to read the
value of the columns of the combo.
You are *too* focussed on .Value property.
Here's an example of reading the columns.

txtAuthorsPhone.Text = cmbAuthorsInitials.Column(6)
So, in other words cmbAuthorsInitials.Column(9) will reflect Typist Intials.

In order to make some sort of generic type of code for you, you will need to
make some preparations.
Y'll have to think of a mechanism that transfers the values from the columns
to the textboxes, resp. the VBA variables.
One mechanism would be to have naming convention of the textboxes, coincide
with the index number of the columns
as explained above.
So the values of 9 columns have to be transfered to the textboxes.
This can be done, for instance by using a names for the textboxes like:
txtAuthorsInitials_1 through to txtAuthorsInitials_9

Textbox txtAuthorsInitials_1 will host author initials, txtAuthorsInitials_4
authors title, and txtAuthorsInitials_8 author's closing....etc
Now you can use a counter to make the match between the columns and the
textboxes, like:

Dim iCol As Integer
icol = cmbAuthorsInitials.ColumnCount
For x = 1 to icol
   If Me.cmbAuthorsInitials.ListIndex = -1 Then
       Me.Controls("txtAuthorsInitials_" & CStr(x).Value = _
           cmbAuthorsInitials.Column(x - 1)
   Else
       MsgBox "Pls select an author"
   End If
Next
'Note the IF test [Me.cmbAuthorsInitials.ListIndex = -1] ensures that there
is a selection
'you will have to ensure that for every column, there's a textbox
"txtAuthorsInitials_X" available!
'and note: columns index of the combobox runs 0 to number of columns minus 1

Hopefully, you can rewrite the code with little effort, ensuring what you
asked for: reduce retyping
Anyhow, I hope that you have got some ideas to automate.

Pls repost if you have difficulties optimising yr code, indicating where you
got stuck.
Good luck

Krgrds,
Perry

> Hi,
>
[quoted text clipped - 108 lines]
>
> End Sub
Kerri - 07 Feb 2007 19:35 GMT
> Hmm, huge text in yr msg.
> Ok, here we go.
[quoted text clipped - 166 lines]
>
> - Show quoted text -

Thank you, Perry, for taking the time to answer.  I will try this and
let you know.
 
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.