Do Until... Help Please!

G

Gina Whipp

Hey Guys (and Gals),

Having fun with Do Until statements, can't get this one to work...

What I want is if you double-click on one field then clear the contents.
What it's doing is asking you the question 9 times instead of just the one
you double click on. Hope that makes sense. Can someone help me?

Thanks in advance,
Gina Whipp



Public Function ClearViolation()
Dim LabelNumber As Long

Do Until LabelNumber = 9
'If IsNull(Me("txt" & LabelNumber & "Label")) Then Exit Function
Dim Msg, Style, Title, Response, MyString

Msg = "Did you want to change this Violation?"
Style = vbYesNo + vbQuestion + vbDefaultButton2
Title = "Violation Type"

Response = MsgBox(Msg, Style, Title)

If Response = vbYes Then
MyString = "Yes"
Me("txt" & LabelNumber & "Label") = ""
Me("txt" & LabelNumber & "Label").Locked = False
Me("txt" & LabelNumber & "Label").BackColor = 13303807
Else
MyString = "No"
DoCmd.CancelEvent
End If

LabelNumber = LabelNumber + 1
Loop
End Function
 
R

Robert Morley

I think maybe you wanted something that looked more like this:

Public Function ClearViolation()
Dim LabelNumber As Long
Dim Msg, Style, Title, Response, MyString

Msg = "Did you want to change this Violation?"
Style = vbYesNo + vbQuestion + vbDefaultButton2
Title = "Violation Type"

Response = MsgBox(Msg, Style, Title)

If Response = vbYes Then
MyString = "Yes"
Do Until LabelNumber = 9
'If IsNull(Me("txt" & LabelNumber & "Label")) Then Exit Function
Me("txt" & LabelNumber & "Label") = ""
Me("txt" & LabelNumber & "Label").Locked = False
Me("txt" & LabelNumber & "Label").BackColor = 13303807
LabelNumber = LabelNumber + 1
Loop
Else
MyString = "No"
'DoCmd.CancelEvent
'Note: commented out the CancelEvent, as I believe it becomes
redundant at this point.
End If
End Function
 
J

John Spencer

Guessing that you really want to do loop through the labels if the question
is answered yes

Try moving the loop inside the If - Then


Public Function ClearViolation()
Dim LabelNumber As Long

'If IsNull(Me("txt" & LabelNumber & "Label")) Then Exit Function
Dim Msg, Style, Title, Response, MyString

Msg = "Did you want to change this Violation?"
Style = vbYesNo + vbQuestion + vbDefaultButton2
Title = "Violation Type"

Response = MsgBox(Msg, Style, Title)

If Response = vbYes Then

Do Until LabelNumber = 9
MyString = "Yes"
Me("txt" & LabelNumber & "Label") = ""
Me("txt" & LabelNumber & "Label").Locked = False
Me("txt" & LabelNumber & "Label").BackColor = 13303807
LabelNumber = LabelNumber + 1
Loop

Else
MyString = "No"
DoCmd.CancelEvent
End If


End Function
 
R

Robert Morley

It occurred to me after I posted that you might be trying to do something
other than what I thought, and obviously John Spencer was thinking the same
thing. If you're trying only to disable one control out of the nine, then
you'd want to do something more like the code below (no looping required).
In this case, you would call the code from the double-click event for each
control and then pass the LabelNumber. Note that there are actually several
optimizations that could be made to the code, if this is what you're trying
to accomplish, but I tried to keep it as similar to your code as possible.

Please let us know if either of the two possible interpretations is what
you're trying to do, or if we've misunderstood entirely. Here's the revised
function:

Public Function ClearViolation(ByVal LabelNumber As Long)
Dim Msg, Style, Title, Response, MyString

Msg = "Did you want to change this Violation?"
Style = vbYesNo + vbQuestion + vbDefaultButton2
Title = "Violation Type"

Response = MsgBox(Msg, Style, Title)

If Response = vbYes Then
MyString = "Yes"
Me("txt" & LabelNumber & "Label") = ""
Me("txt" & LabelNumber & "Label").Locked = False
Me("txt" & LabelNumber & "Label").BackColor = 13303807
Else
MyString = "No"
End If
End Function
 
G

Gina Whipp

John Spencer,

I unremmed the...

If IsNull(Me("txt" & LabelNumber & "Label")) Then Exit Function

and while it breaks at that point it still runs the code, am I missing
something?

Thanks,
Gina Whipp
 
G

Gina Whipp

But where is it getting the numbers from, now I am confused.

But to go back... The code you both provided does what I want except it
does not stop if the field is empty. I was trying to write a
ClearViolation() that could be called from the double-click event of each
control but if the control was empty to do nothing.

Hope I explained that correctly and clearly!
Gina Whipp
 
G

Gina Whipp

You know I thought all was well until I answered yes then all the fields in
the record went blank. I think I'm going back to the simplier code for each
control, much easier, so here's what I came up with....

If IsNull([txt1Label]) Then Exit Sub

Dim Msg, Style, Title, Response, MyString
Msg = "Did you want to change this Violation?"
Style = vbYesNo + vbQuestion + vbDefaultButton2
Title = "Violation Type"

Response = MsgBox(Msg, Style, Title)

If Response = vbYes Then
MyString = "Yes"
Me.txt1Label = ""
Me.txt1Label.Locked = False
Me.txt1Label.BackColor = 13303807
Else
MyString = "No"
DoCmd.CancelEvent
End If
 
R

Robert Morley

Okay, if that was your code for one, then this would be your code for each
(this is the optimized version I hinted at earlier):

Public Sub ClearViolation(ByRef LabelControl As Label)
If Nz(LabelControl.Caption) <> "" Then 'Note, this handles both null and
empty strings in your control.
'You should always specifically declare the types of any variables,
both for speed and readability.
Dim Msg As String
Dim Style As VbMsgBoxStyle
Dim Title As String
Dim Response As VbMsgBoxResult
Dim MyString As String 'Is this even necessary?

Msg = "Did you want to change this Violation?"
Style = vbYesNo + vbQuestion + vbDefaultButton2
Title = "Violation Type"

Response = MsgBox(Msg, Style, Title)

If Response = vbYes Then
MyString = "Yes"
With LabelControl
.Caption = ""
.Locked = False
.BackColor = 13303807
End With
Else
MyString = "No"
End If
End If
End Sub

Then wherever you need to call it from (I'm assuming it's the label's
double-click event), you would do the following:

Private Sub txt1Label_DblClick(Cancel As Integer)
ClearViolation Me!txt1Label
End Sub

Private Sub txt2Label_DblClick(Cancel As Integer)
ClearViolation Me!txt2Label
End Sub

....etc.




Rob

Gina Whipp said:
You know I thought all was well until I answered yes then all the fields
in the record went blank. I think I'm going back to the simplier code for
each control, much easier, so here's what I came up with....

If IsNull([txt1Label]) Then Exit Sub

Dim Msg, Style, Title, Response, MyString
Msg = "Did you want to change this Violation?"
Style = vbYesNo + vbQuestion + vbDefaultButton2
Title = "Violation Type"

Response = MsgBox(Msg, Style, Title)

If Response = vbYes Then
MyString = "Yes"
Me.txt1Label = ""
Me.txt1Label.Locked = False
Me.txt1Label.BackColor = 13303807
Else
MyString = "No"
DoCmd.CancelEvent
End If




Robert Morley said:
It occurred to me after I posted that you might be trying to do something
other than what I thought, and obviously John Spencer was thinking the
same thing. If you're trying only to disable one control out of the
nine, then you'd want to do something more like the code below (no
looping required). In this case, you would call the code from the
double-click event for each control and then pass the LabelNumber. Note
that there are actually several optimizations that could be made to the
code, if this is what you're trying to accomplish, but I tried to keep it
as similar to your code as possible.

Please let us know if either of the two possible interpretations is what
you're trying to do, or if we've misunderstood entirely. Here's the
revised function:

Public Function ClearViolation(ByVal LabelNumber As Long)
Dim Msg, Style, Title, Response, MyString

Msg = "Did you want to change this Violation?"
Style = vbYesNo + vbQuestion + vbDefaultButton2
Title = "Violation Type"

Response = MsgBox(Msg, Style, Title)

If Response = vbYes Then
MyString = "Yes"
Me("txt" & LabelNumber & "Label") = ""
Me("txt" & LabelNumber & "Label").Locked = False
Me("txt" & LabelNumber & "Label").BackColor = 13303807
Else
MyString = "No"
End If
End Function
 
J

John Spencer

You probably need to move that line inside the loop?

Perhaps you should take a step back and try to write out a description of
exactly what you want this function to do.
 
G

Gina Whipp

Thanks Robert, I like the optimization... and will be using it!

Gina Whipp


Robert Morley said:
Okay, if that was your code for one, then this would be your code for each
(this is the optimized version I hinted at earlier):

Public Sub ClearViolation(ByRef LabelControl As Label)
If Nz(LabelControl.Caption) <> "" Then 'Note, this handles both null
and empty strings in your control.
'You should always specifically declare the types of any variables,
both for speed and readability.
Dim Msg As String
Dim Style As VbMsgBoxStyle
Dim Title As String
Dim Response As VbMsgBoxResult
Dim MyString As String 'Is this even necessary?

Msg = "Did you want to change this Violation?"
Style = vbYesNo + vbQuestion + vbDefaultButton2
Title = "Violation Type"

Response = MsgBox(Msg, Style, Title)

If Response = vbYes Then
MyString = "Yes"
With LabelControl
.Caption = ""
.Locked = False
.BackColor = 13303807
End With
Else
MyString = "No"
End If
End If
End Sub

Then wherever you need to call it from (I'm assuming it's the label's
double-click event), you would do the following:

Private Sub txt1Label_DblClick(Cancel As Integer)
ClearViolation Me!txt1Label
End Sub

Private Sub txt2Label_DblClick(Cancel As Integer)
ClearViolation Me!txt2Label
End Sub

...etc.




Rob

Gina Whipp said:
You know I thought all was well until I answered yes then all the fields
in the record went blank. I think I'm going back to the simplier code
for each control, much easier, so here's what I came up with....

If IsNull([txt1Label]) Then Exit Sub

Dim Msg, Style, Title, Response, MyString
Msg = "Did you want to change this Violation?"
Style = vbYesNo + vbQuestion + vbDefaultButton2
Title = "Violation Type"

Response = MsgBox(Msg, Style, Title)

If Response = vbYes Then
MyString = "Yes"
Me.txt1Label = ""
Me.txt1Label.Locked = False
Me.txt1Label.BackColor = 13303807
Else
MyString = "No"
DoCmd.CancelEvent
End If




Robert Morley said:
It occurred to me after I posted that you might be trying to do
something other than what I thought, and obviously John Spencer was
thinking the same thing. If you're trying only to disable one control
out of the nine, then you'd want to do something more like the code
below (no looping required). In this case, you would call the code from
the double-click event for each control and then pass the LabelNumber.
Note that there are actually several optimizations that could be made to
the code, if this is what you're trying to accomplish, but I tried to
keep it as similar to your code as possible.

Please let us know if either of the two possible interpretations is what
you're trying to do, or if we've misunderstood entirely. Here's the
revised function:

Public Function ClearViolation(ByVal LabelNumber As Long)
Dim Msg, Style, Title, Response, MyString

Msg = "Did you want to change this Violation?"
Style = vbYesNo + vbQuestion + vbDefaultButton2
Title = "Violation Type"

Response = MsgBox(Msg, Style, Title)

If Response = vbYes Then
MyString = "Yes"
Me("txt" & LabelNumber & "Label") = ""
Me("txt" & LabelNumber & "Label").Locked = False
Me("txt" & LabelNumber & "Label").BackColor = 13303807
Else
MyString = "No"
End If
End Function


Hey Guys (and Gals),

Having fun with Do Until statements, can't get this one to work...

What I want is if you double-click on one field then clear the
contents. What it's doing is asking you the question 9 times instead of
just the one you double click on. Hope that makes sense. Can someone
help me?

Thanks in advance,
Gina Whipp



Public Function ClearViolation()
Dim LabelNumber As Long

Do Until LabelNumber = 9
'If IsNull(Me("txt" & LabelNumber & "Label")) Then Exit Function
Dim Msg, Style, Title, Response, MyString

Msg = "Did you want to change this Violation?"
Style = vbYesNo + vbQuestion + vbDefaultButton2
Title = "Violation Type"

Response = MsgBox(Msg, Style, Title)

If Response = vbYes Then
MyString = "Yes"
Me("txt" & LabelNumber & "Label") = ""
Me("txt" & LabelNumber & "Label").Locked = False
Me("txt" & LabelNumber & "Label").BackColor = 13303807
Else
MyString = "No"
DoCmd.CancelEvent
End If

LabelNumber = LabelNumber + 1
Loop
End Function
 
G

Gina Whipp

John,

Tried that but no matter switched to what Robert had suggested and it works
just fine.

Thanks,
Gina Whipp
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top