Problem with For...Next statements

jguscs

Registered User.
Local time
Today, 10:54
Joined
Jun 23, 2003
Messages
148
The following code works its way through some simple If...Else...EndIf statements and For...Next statements.
Me.AddDate is Null.
The result of running the code (with the MsgBox "markers") is that the following messages appear (in the order listed):
1) End Add True
2) End Add Checks
3) End Add True
4) End Add Checks
...I would have thought that the message boxes should display:
1) End Add False
2) End Add Checks
What's going on here?!?!

If Not IsNull(Me.AddDate) Then
Dim AddTrue As Control
For Each AddTrue In Me.Controls
If Left(AddTrue.Name, 3) = "Add" Then
AddTrue.Visible = True
End If
Next AddTrue
Me!CheckAdd = True
MsgBox "End Add True"
Else
Dim AddFalse As Control
For Each AddFalse In Me.Controls
If Left(AddFalse.Name, 3) = "Add" Then
AddFalse.Visible = False
End If
Next AddFalse
Me!CheckAdd = False
MsgBox "End Add False"
End If
MsgBox "End Add Checks"
 
Oy!


PHP:
Private Sub Form_Current()
Dim AddTrue As Control
Dim AddFalse As Control

    If Not IsNull(Me.AddDate) Then
        For Each AddTrue In Me.Controls
            If Left(AddTrue.Name, 3) = "Add" Then
                AddTrue.Visible = True
            End If
        Next AddTrue
        Me!CheckAdd = True
        MsgBox "End Add True"
    Else
        For Each AddFalse In Me.Controls
            If Left(AddFalse.Name, 3) = "Add" Then
                AddFalse.Visible = False
            End If
        Next AddFalse
        Me!CheckAdd = False
        MsgBox "End Add False"
    End If
    MsgBox "End Add Checks"

End Sub

What types of controls are we dealing with here? What is AddDate?

Put a 'marker' before your first conditional test to see what the true value of AddDate is:
MsgBox Not IsNull(Me.AddDate)

As you can see, I put it in the form's OnCurrent event. Where do you have the code?
 
Sorry I couldn't get back to you sooner, pdx_man.
To answer your questions:

1) The types of controls I am trying to work with are text boxes and check boxes on a report.

2) AddDate is the value of a text box that will be given upon report generation using a record source that may or may not have an entry for AddDate.
IE: If AddDate has a value, all controls that are named with the first 3 characters "Add" will be visible. If AddDate is NULL, all controls that are named beginning with the first 3 characters "Add" will be invisible.

3) I have the code in the format procedure of the report's detail section.

4) I put your marker code "MsgBox Not IsNull(Me.AddDate)" before the first conditional statement and it spat out "True" (which is the expected result, as the record source does not have a value in its AddDate field to transmit to the AddDate textbox control).

I've included the entire procedure below this time. Hope that helps. It's just more of the same though.

PHP:
Private Sub Detail_Format(Cancel As Integer, FormatCount As Integer)
MsgBox Not IsNull(Me.AddDate)
    If Not IsNull(Me.AddDate) Then
        Dim AddTrue As Control
        For Each AddTrue In Me.Controls
            If Left(AddTrue.Name, 3) = "Add" Then
                AddTrue.Visible = True
            End If
        Next AddTrue
        MsgBox "End Add True"
        Me!CheckAdd = True
    Else
        Dim AddFalse As Control
        For Each AddFalse In Me.Controls
            If Left(AddFalse.Name, 3) = "Add" Then
                AddFalse.Visible = False
            End If
        Next AddFalse
        MsgBox "End Add False"
        Me!CheckAdd = False
    End If
    MsgBox "End Add Checks"
    If Not IsNull(Me.DelDate) Then
        Dim DelTrue As Control
        For Each DelTrue In Me.Controls
            If Left(DelTrue.Name, 3) = "Del" Then
                DelTrue.Visible = True
            End If
        Next DelTrue
        Me!CheckDel = True
    Else
        Dim DelFalse As Control
        For Each DelFalse In Me.Controls
            If Left(DelFalse.Name, 3) = "Del" Then
                DelFalse.Visible = False
            End If
        Next DelFalse
        Me!CheckDel = False
    End If
    If Not IsNull(Me.ChgDate) Then
        Dim ChgTrue As Control
        For Each ChgTrue In Me.Controls
            If Left(ChgTrue.Name, 3) = "Chg" Then
                ChgTrue.Visible = True
            End If
        Next ChgTrue
        Me!CheckChg = True
    Else
        Dim ChgFalse As Control
        For Each ChgFalse In Me.Controls
            If Left(ChgFalse.Name, 3) = "Chg" Then
                ChgFalse.Visible = False
            End If
        Next ChgFalse
        Me!CheckChg = False
    End If
End Sub
 
Are you familiar with Breakpoints and their use in debugging? Go to the first MsgBox line and hit your [F9] key. This will toggle a breakpoint on this line. Then run the report. Execution will pause on this line. Then you can hit the [F8] key to step through your code. Hover over a variable to get a pop-up of the value. Select a variable and hit [shift-F9] to get a Watch of the variable. Step through your code and see how it executes and what the values are. I'm guessing the values just aren't what you are expecting.
 
Thanks, actually I did not know about break points.
I'll try that.
In the mean time, I reasoned that the values are not what I'm expecting.
For example, AddDate is not NULL. I don't know what the value is for a record that is empty that has been assigned to a control (text box), but the code for the case when AddDate is NULL never occurs.
I tried checking to see whether AddDate is = "" (empty string), but that didn't work either.
I'll try to check the length of AddDate next.
If LEN(AddDate) > 0 Then
...
 
OK, I tried the debugging you told me about, and the most helpful aspect of it is the hovering over a variable part.
I'm still having trouble with figuring out how to check the value of AddDate if the field has not been set to any value because its corresponding record is empty.
So, instead of doing an IsNull check, I do an IsEmpty check and it still doesn't work properly.
 
Last edited:
amazing. it worked.
in fact, it works better than IsNull.

The table I was using for my recordsource was completely empty, except for its column headers and one blank row.
Using the IsNull or IsEmpty method, for some strange reason, I needed to put TWO blank rows in the table. THEN those methods would work for checking to see whether any data was available.

However, using the IsDate function, I don't even need 2 rows.
 

Users who are viewing this thread

Back
Top Bottom