Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

VB Beginner Needs Coding Suggestion

Status
Not open for further replies.

BradB

MIS
Jun 21, 2001
237
US
I’m absolutely sure I’m doing this the hard way. The following code is driving me crazy. It works okay, but is a programming nightmare. I seem to always have to do things the hard way. Is there a better way of coding the below arguments? All I want is to have certain text boxes visible/not visible. The same goes for enable/disable. Can anyone give me a few pointers?

Option Compare Database
Private Sub cmdDateNotif_Click()
txtDateNotified.Value = Now()
End Sub

Private Sub cmdDateRec_Click()
txtDateRec.Value = Now()
End Sub
Private Sub cmdPostedDate_Click()
txtPostingDate.Value = Now()
End Sub

Private Sub cmdHoldDate_Click()
txtHoldDate.Value = Now()
End Sub

Private Sub Form_Current()
If chkNotified.Value = True Then txtDateNotified.Visible = True Else cmdDateNotif.Visible = False
If chkNotified.Value = False Then txtDateNotified.Visible = False Else cmdDateNotif.Visible = True
If chkPosted.Value = True Then txtPostingDate.Visible = True Else cmdPostedDate.Visible = False
If chkPosted.Value = False Then txtPostingDate.Visible = False Else cmdPostedDate.Visible = True
If chkHold.Value = True Then txtHoldDate.Visible = True Else cmdHoldDate.Visible = False
If chkHold.Value = False Then txtHoldDate.Visible = False Else cmdHoldDate.Visible = True


If chkNotified.Value = True Then txtDateNotified.Visible = True Else txtDateNotified.Visible = False
If chkPosted.Value = True Then txtPostingDate.Visible = True Else txtPostingDate.Visible = False
If chkHold.Value = True Then txtHoldDate.Visible = True Else txtHoldDate.Visible = False
Me.Staff.Value = Forms!frmStaffInfo!StaffID

End Sub

Private Sub Form_Load()
If chkNotified.Value = True Then txtDateNotified.Visible = True Else cmdDateNotif.Visible = False
If chkPosted.Value = True Then txtPostingDate.Visible = True Else cmdPostedDate.Visible = False
If chkHold.Value = True Then txtHoldDate.Visible = True Else cmdHoldDate.Visible = False

If chkNotified.Value = True Then txtDateNotified.Visible = True Else txtDateNotified.Visible = False
If chkPosted.Value = True Then txtPostingDate.Visible = True Else txtPostingDate.Visible = False
If chkHold.Value = True Then txtHoldDate.Visible = True Else txtHoldDate.Visible = False

Me.Staff.Value = Forms!frmStaffInfo!StaffID
End Sub
Private Sub chkNotified_Click()
If chkNotified.Value = True Then txtDateNotified.Visible = True Else cmdDateNotif.Visible = False
If chkNotified.Value = True Then txtDateNotified.Visible = True Else txtDateNotified.Visible = False
If chkNotified.Value = False Then txtDateNotified.Visible = False Else cmdDateNotif.Visible = True
If chkNotified.Value = False Then txtDateNotified.Visible = False Else txtDateNotified.Visible = True
End Sub
Private Sub chkPosted_Click()
If chkPosted.Value = True Then txtPostingDate.Visible = True Else cmdPostedDate.Visible = False
If chkPosted.Value = True Then txtPostingDate.Visible = True Else txtPostingDate.Visible = False
If chkPosted.Value = False Then txtPostingDate.Visible = False Else cmdPostedDate.Visible = True
If chkPosted.Value = False Then txtPostingDate.Visible = False Else txtPostingDate.Visible = True
End Sub
Private Sub chkHold_Click()
If chkHold.Value = True Then txtHoldDate.Visible = True Else cmdHoldDate.Visible = False
If chkHold.Value = True Then txtHoldDate.Visible = True Else txtHoldDate.Visible = False
If chkHold.Value = False Then txtHoldDate.Visible = False Else cmdHoldDate.Visible = True
If chkHold.Value = False Then txtHoldDate.Visible = False Else txtHoldDate.Visible = True
End Sub

 
when faced with these situations i write a sub to 'neaten' the code:

private sub IsItVisible(ChkBox as CheckBox, AffectIfTrue as Control, AffectIfFalse as Control)

If ChkBox.Value = True then
AffectIfTrue.Visible = True
else
AffectIfFalse.Visible = False
End if
end sub

use .Enabled for the enabled / disabled if you like.


 
holy sheesh, yes, way easier way to simplify that code...thanks for that little bit of code richard...I didn't know you could make a sub that you could call a control by. Regards,
Anth:cool:ny
----------------------------------------
"You say [red]insanity[/red] like it's a BAD THING!"
 
You can simplify it a lot further...

private sub ChangeVisible(ChkBox as CheckBox, TrueControl as Control, FalseControl as Control)

' rely on default value - oh, and the fact that Checkboxes don't -as you might expect - actually support genuine boolean True/False values
TrueControl.Visible = -ChkBox
FalseControl.Visible = ChkBox
end sub
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top