How do I say "unless" in VBA?

BearsInNeed

New member
Local time
Yesterday, 16:15
Joined
Aug 6, 2013
Messages
6
Hello there!
I have the following problem:
I have a form that automatically puts a value into a field if I click a checkbox. So far, so good. I want the field to say 0, unless the check box is clicked, in which case it will automatically write 12 into the field. I wrote a piece of code that does almost that. But as the action taken is ‘click’ it only puts the 0 in, when I click and unclick the checkbox. I can’t figure out how to write something like: I want this field to say 0, unless I click the Check box, in which case I’d like the field to say 12.
This is the code that I came up with:
Private Sub Tablecloth_Click()
If [Forms]![Bare campsite Data Entry Form]![Tablecloth] = -1 Then
[Forms]![Bare campsite Data Entry Form]![Tablecloth_hazard] = 12
Else
[Forms]![Bare campsite Data Entry Form]![Tablecloth_hazard] = 0
End If
End Sub
I am a complete novice but somehow got into the situation where I have to build this database from scratch and I want it to be neat, self explanatory and highly functional, as other people will be using it.
To put it mildly, I’m in over my head, so would appreciate any help.
Thank you!
 
Hello there!
I have the following problem:
I have a form that automatically puts a value into a field if I click a checkbox. So far, so good. I want the field to say 0, unless the check box is clicked, in which case it will automatically write 12 into the field. I wrote a piece of code that does almost that. But as the action taken is ‘click’ it only puts the 0 in, when I click and unclick the checkbox. I can’t figure out how to write something like: I want this field to say 0, unless I click the Check box, in which case I’d like the field to say 12.
This is the code that I came up with:
Private Sub Tablecloth_Click()
If [Forms]![Bare campsite Data Entry Form]![Tablecloth] = -1 Then
[Forms]![Bare campsite Data Entry Form]![Tablecloth_hazard] = 12
Else
[Forms]![Bare campsite Data Entry Form]![Tablecloth_hazard] = 0
End If
End Sub
I am a complete novice but somehow got into the situation where I have to build this database from scratch and I want it to be neat, self explanatory and highly functional, as other people will be using it.
To put it mildly, I’m in over my head, so would appreciate any help.
Thank you!


Code:
 [/FONT]
[FONT=Arial]Private Sub Tablecloth_Click()[/FONT]
[FONT=Arial]If [Forms]![Bare campsite Data Entry Form]![Tablecloth] = TRUE Then[/FONT]
[FONT=Arial][Forms]![Bare campsite Data Entry Form]![Tablecloth_hazard] = 12[/FONT]
[FONT=Arial]ElseIf [Forms]![Bare campsite Data Entry Form]![Tablecloth] = FALSE Then[/FONT]
[FONT=Arial][Forms]![Bare campsite Data Entry Form]![Tablecloth_hazard] = 0[/FONT]
[FONT=Arial]End If[/FONT]
[FONT=Arial]End Sub

You'd probably need to set the default value of [Forms]![Bare campsite Data Entry Form]![Tablecloth_hazard]

to 0
 
I'd use the AfterUpdate event for this, rather than the OnClick, and you don't need to give a complete reference to the Form, when the code in that Form's Code Module, you can use the Me. construct, instead. One way to set the Tablecloth_hazard for Tablecloth_hazard to Zero would be

Code:
Private Sub Form_Current()
 If Me.NewRecord Then Me.Tablecloth_hazard = 0
End Sub

Then for the assignment of value:

Code:
Private Sub TableCloth_AfterUpdate()
 
 If Me.TableCloth = -1 Then
   Me.[Tablecloth_hazard] = 12
 Else
   Me.[Tablecloth_hazard] = 0
 End If

End Sub

Linq ;0)>
 
Maybe I don't understand well the scenario so, forgive me, please, if I am wrong:
I think that that field (where you store 0 or 12) should be a Yes/No field and the check box should be bound to it.
Then, only for visual information, a text box with the control Source:
= Me.CheckBoxName * (-12)
or
= IIF(Me.CheckBoxName,12,0)
 
Boy, do I feel like an idiot now..... The default value was it!
The true/false change to the code works, too. Not sure which is better as both, that one and mine seem to work now!
As for the 'Me' construct: Thank you, I always wondered what that stood for. It'll make my next codes way more readable.
Bottom line:
Thank you soooo much for your help. And your speedy help at that. I can't tell you how grateful I am!
 
My first intention was to remind you about the default value property, but is not a very good idea because involve a lot of code:
- to check every time (On Current event) if the field is 0 or 12 in order to update the checkbox
- to code the checkbox in order to return 0 or 12 then to update the field.
 

Users who are viewing this thread

Back
Top Bottom