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 / October 2006

Tip: Looking for answers? Try searching our database.

if-andif-then statement - need help

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Susan - 04 Oct 2006 15:15 GMT
i'm sure there's some way to do this, but i can't figure it out.
if .chkahc is true the 1st time, then do the 1st IF (enable
pgAdditional & exit sub).
since this now exits the sub, i need to get past this part the 2nd time
i run the code.......
if .chkahc is true AND pgAdditional is already enabled AND these fields
are not blank, then continue the code without the message box & exiting
the sub.
can somebody please help me with the structure? (code below)
thank you!
susan
xxxxxxxxxxxxxxxxxxxxxxx

With Me
   If .chkAHC.Value = True Then
      .MultiPage1("pgAdditional").Enabled = True
      .MultiPage1.Value = 2
      .MultiPage1("pgAdditional").txtSection.SetFocus
            With cboYears
               .AddItem "Two"
               .AddItem "Five"
               .AddItem "Ten"
           End With
           cboYears.Value = ""

      MsgBox "Please fill in additional AHC information", _
           vbOKOnly + vbInformation
      Exit Sub

'    ElseIf .chkAHC.Value = True Then
'        If .txtSection <> "" Then
'        If .txtBlock <> "" Then
'        If .txtLot <> "" Then
'        If .cboYears <> "" Then
'        what?  continue code???
   End If
Russ - 04 Oct 2006 18:07 GMT
Susan,
You set up a boolean variable called something like blnFirst and initialize
it to True where the code only gets run once at the beginning and then use
it this way:
If .chkAHC.Value And blnFirst then
...first time code
blnFirst = False
...more code?
End If

> i'm sure there's some way to do this, but i can't figure it out.
> if .chkahc is true the 1st time, then do the 1st IF (enable
[quoted text clipped - 32 lines]
> '        what?  continue code???
>     End If

Signature

Russ

drsmN0SPAMikleAThotmailD0Tcom.INVALID

Susan - 04 Oct 2006 19:36 GMT
thank you for your help.........

so up @ the top of my command code, i entered:

Dim blnFirst As Boolean
Set blnFirst = True  'xxxx

xxx = but this line trips an error - says "object required" - i also
added the dim line to the initializing code since you mentioned it had
to be initialized - that didn't work.  also added the "set" statement
up in the initialization, too, but then it just targets that 1st set
statement as the error.  maybe i don't have the structure correct.

and then the second "end if" would go way at the bottom of all my other
code, correct?

so the first time it runs through it automatically trips blnFirst to
false?
thanks again
susan
PCAssist-NW_JimR - 05 Oct 2006 05:55 GMT
That's because the keyword SET is used with objects.  Remove SET and it
should work fine.

Jim R.

> thank you for your help.........
>
[quoted text clipped - 16 lines]
> thanks again
> susan
PCAssist-NW_JimR - 05 Oct 2006 06:08 GMT
Remove the SET keyword before the blnFirst variable and it should work as you
want it to work.

Jim Rice

> thank you for your help.........
>
[quoted text clipped - 16 lines]
> thanks again
> susan
Russ - 05 Oct 2006 09:09 GMT
Susan,
> thank you for your help.........
>
> so up @ the top of my command code, i entered:
>
> Dim blnFirst As Boolean
> Set blnFirst = True  'xxxx
Variables don't use a 'set' syntax word to make them equal to some value.
It's just;
blnFirst = True

> xxx = but this line trips an error - says "object required" - i also
Because objects do need the 'set' syntax word and the parser was expecting
an object after seeing 'set' in that line of code, but read a variable
instead. Thus the error message.
> added the dim line to the initializing code since you mentioned it had
> to be initialized - that didn't work.
Yes, 'Dim blnFirst As Boolean' and make it equal to True in the
initialization routine that only gets run once, every time the form starts.
>  also added the "set" statement
> up in the initialization, too, but then it just targets that 1st set
> statement as the error.  maybe i don't have the structure correct.
>
> and then the second "end if" would go way at the bottom of all my other
> code, correct?
There is no 'second' "end if", unless you require it. I was just trying to
show that you put the blnFirst = False in where ever it will be executed the
first time through. So that the blnFirst test will fail after the first time
through and skip over the code and go to the ElseIf clause (provided that is
the way you leave the original code, by having a ElseIf clause.)

> so the first time it runs through it automatically trips blnFirst to
> false?
Exactly!
> thanks again
> susan

>------------------- Clipped Text ------------------------
> Susan,
[quoted text clipped - 45 lines]
>>     End If
>------------------------------------------------------
Signature

Russ

drsmN0SPAMikleAThotmailD0Tcom.INVALID

Susan - 05 Oct 2006 13:21 GMT
thanks russ & jim........
got that variable done correctly.
also figured out the if-endif issue.

please forgive my denseness on the boolean value - 1st time using it.
in the initializing i did blnFirst=True.
i also HAD to enter blnFirst=True at the beginning of my command macro,
because if i don't, it doesn't pick up on the statement:
   If .chkAHC.Value = True And blnFirst = True Then
i tested this several times.

near the bottom of this if-then indented section, i added
blnFirst=False.
however, when it restarts & runs from the top again, it sets it back to
true.
please help me understand what i'm doing wrong.
thank you
susan
Russ - 05 Oct 2006 18:10 GMT
Susan,
Show us the code where you trying to use blnFirst. It may be that you are
declaring command subroutines or the variables 'Private', instead of
'Public' (which narrows the 'scope' of their variable usage to just that
subroutine.) Or that you should declare the boolean variable in the
(Declarations) section of your project like these two lines...
Option Explicit
Public blnFirst As Boolean
... In order for the 'scope' of the variable to be more 'global' (to be able
to be seen by all subroutines.)
Read about 'scope' of variables in VBA Help.
> thanks russ & jim........
> got that variable done correctly.
[quoted text clipped - 4 lines]
> i also HAD to enter blnFirst=True at the beginning of my command macro,
> because if i don't, it doesn't pick up on the statement:
You don't want it at the beginning of the command macro that you are testing
it in, because each time it goes through that macro and not just the first
time it is going to equate to True in your test. See the issue about scope
above.
So in order for this technique to work, it must be set to True in the
userform initialization subroutine; and tested and subsequently set to False
in your other userform command button subroutine. Then since it is never set
to True in the command button subroutine, after the first time through, the
command button test will always read as False when you click on the button.
Do you see the logic in doing it this way?
>     If .chkAHC.Value = True And blnFirst = True Then
Set it to False right after this line to make sure it gets done immediately
after the testing and is flipped from True to False. You don't want to
accidentally put after a Else or ElseIf clause unless you intend to do so in
the logical flow of your subroutine.
> i tested this several times.
>
[quoted text clipped - 5 lines]
> thank you
> susan

Signature

Russ

drsmN0SPAMikleAThotmailD0Tcom.INVALID

Susan - 05 Oct 2006 18:47 GMT
> Susan,
> Show us the code where you trying to use blnFirst.

ok, you asked for it <cringe>!
i'm not giving you the whole code past opening the 1st file, cuz it's
ridiculously long & repetitive.  & yes, it looks like it might be the
"private" sub issue.
(of course, so is this, but you asked for it).
:)   susan
xxxxxxxxxxxxxxxxxxxxxxxxx
Option Explicit

Private Sub cmdClose_Click()
 Unload Me
End Sub

Private Sub cmdNext_Click()

   If txtYear.Value = "" Then
   MsgBox "You haven't entered the year!" _
       , vbOKOnly + vbCritical
       txtYear.SetFocus
       Exit Sub
   End If

   If txtCounty.Value = "" Then
   MsgBox "You haven't entered the county!" _
       , vbOKOnly + vbCritical
       txtCounty.SetFocus
       Exit Sub
   End If

   If txt1stName.Value = "" Then
   MsgBox "You haven't entered the client's first name!" _
       , vbOKOnly + vbCritical
       txt1stName.SetFocus
       Exit Sub
   End If

   If txt2ndName.Value = "" Then
   MsgBox "You haven't entered the client's last name!" _
       , vbOKOnly + vbCritical
       txt2ndName.SetFocus
       Exit Sub
   End If

   If txtAddress.Value = "" Then
   MsgBox "You haven't entered the address!" _
       , vbOKOnly + vbCritical
       txtAddress.SetFocus
       Exit Sub
   End If

   If txtTown.Value = "" Then
   MsgBox "You haven't entered the town!" _
       , vbOKOnly + vbCritical
       txtTown.SetFocus
       Exit Sub
   End If

   If txtRecord.Value = "" Then
   MsgBox "You haven't entered the deed recording date!" _
       , vbOKOnly + vbCritical
       txtRecord.SetFocus
       Exit Sub
   End If

   If txtBook.Value = "" Then
   MsgBox "You haven't entered the deed book number!" _
       , vbOKOnly + vbCritical
       txtBook.SetFocus
       Exit Sub
   End If

   If txtPage.Value = "" Then
   MsgBox "You haven't entered the deed page/instrument number!" _
       , vbOKOnly + vbCritical
       txtPage.SetFocus
       Exit Sub
   Else
   Me.MultiPage1("pgChoose").Enabled = True
   Me.MultiPage1.Value = 1
   Me.MultiPage1("pgChoose").chkLCHOME.SetFocus
   End If

End Sub

Private Sub frmGenerateMortgages_Initialize()

Dim chkAHC As Control
Dim chkNCHC As Control
Dim chkLCHOME As Control
Dim chkHPG As Control
Dim iNCHCAmt As Integer
Dim iLCHOMEAmt As Integer
Dim iHPGAmt As Integer
Dim iYear As Integer
Dim sCounty As String
Dim sName1 As String
Dim sName2 As String
Dim sAddress As String
Dim sTown As String
Dim sVillage As String
Dim dRecord As Date
Dim iBook As Integer
Dim iPage As Integer
Dim sMtg1 As String
Dim sMtg2 As String
Dim blnFirst As Boolean

txtYear.Value = ""
txtCounty.Value = ""
txt1stName.Value = ""
txt2ndName.Value = ""
txtAddress.Value = ""
txtTown.Value = ""
txtVillage.Value = ""
txtRecord.Value = ""
txtBook.Value = ""
txtPage.Value = ""
txtMtg1.Value = ""
txtMtg2.Value = ""
txtSection.Value = ""
txtBlock.Value = ""
txtLot.Value = ""

   With cboYears
       .AddItem "Two"
       .AddItem "Five"
       .AddItem "Ten"
   End With

cboYears.Value = ""

   With Me
   MultiPage1.Value = 0
   txtYear.SetFocus
   End With

End Sub

Private Sub cmdGenerate_Click()

Dim chkAHC As Control
Dim chkNCHC As Control
Dim chkLCHOME As Control
Dim chkHPG As Control
Dim iNCHCAmt As Integer
Dim iLCHOMEAmt As Integer
Dim iHPGAmt As Integer
Dim iYear As Integer
Dim sCounty As String
Dim sName1 As String
Dim sName2 As String
Dim sAddress As String
Dim sTown As String
Dim sVillage As String
Dim dRecord As Date
Dim iBook As Integer
Dim iPage As Integer
Dim sMtg1 As String
Dim sMtg2 As String
Dim blnFirst As Boolean

With Me
   If .chkAHC.Value = True And blnFirst = True Then
      blnFirst = False
      .MultiPage1("pgAdditional").Enabled = True
      .MultiPage1.Value = 2
      .MultiPage1("pgAdditional").txtSection.SetFocus
            With cboYears
               .AddItem "Two"
               .AddItem "Five"
               .AddItem "Ten"
           End With

           cboYears.Value = ""
      MsgBox "Please fill in additional AHC information", _
           vbOKOnly + vbInformation
      Exit Sub
   End If

       If blnFirst = False Then

           If .chkNCHC.Value = True Then
               ChangeFileOpenDirectory _
                   "\\Server\susan\My Documents\Note & Mortgages\"
               Documents.Open FileName:="""DANC Snowbelt Gt &
Mortgage.doc""", _
                   ConfirmConversions:=True, ReadOnly:=False,
AddToRecentFiles:=False, _
                   PasswordDocument:="", PasswordTemplate:="",
Revert:=False, _
                   WritePasswordDocument:="",
WritePasswordTemplate:="", Format:= _
                   wdOpenFormatAuto

blah blah blah more code

           End if
       End If
End With
   
End Sub
Susan - 05 Oct 2006 19:48 GMT
you know what?  ignore that last post.....
i read up on public vs. private scope, & i think that is the issue.
i'm going to keep working with it & if/when i can't solve it, or i run
into a new issue, i'll start a new post.
thanks a lot for your help!
susan
Russ - 06 Oct 2006 08:09 GMT
Susan,

>> Susan,
>> Show us the code where you trying to use blnFirst.
[quoted text clipped - 84 lines]
>
> Private Sub frmGenerateMortgages_Initialize()
This is what I meant by the initialization subroutine.
This is the subroutine that gets read once when the form starts. So set
blnFirst = True here. So it only gets set to True once at the beginning.

> Dim chkAHC As Control
> Dim chkNCHC As Control
[quoted text clipped - 16 lines]
> Dim sMtg2 As String
> Dim blnFirst As Boolean
blnFirst = True

> txtYear.Value = ""
> txtCounty.Value = ""
[quoted text clipped - 49 lines]
> Dim sMtg2 As String
> Dim blnFirst As Boolean
Shouldn't need to declare here it, also.
Already declared above in initialization subroutine and set to True there,
too.

> With Me
>     If .chkAHC.Value = True And blnFirst = True Then
Now it will only be True the first time through each time the userform is
started because you flip it to False in next statement. Of course,
.chkAHC.Value *also* has to be True in order to execute the next line.
>        blnFirst = False
>        .MultiPage1("pgAdditional").Enabled = True
[quoted text clipped - 34 lines]
>    
> End Sub

Signature

Russ

drsmN0SPAMikleAThotmailD0Tcom.INVALID


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.