VB School Assignment? Project Due Tomorrow? Chat LIVE With A Programming Expert!

Welcome to Dream.In.Code
Become a VB Expert!

Join 300,431 VB Programmers for FREE! Get instant access to thousands of VB experts, tutorials, code snippets, and more! There are 1,503 people online right now. Registration is fast and FREE... Join Now!




Reducing the use of repetitive codes

 

Reducing the use of repetitive codes, I don't know how to explain this but

Kinnison

27 Jun, 2009 - 04:09 PM
Post #1

New D.I.C Head
*

Joined: 24 Jun, 2009
Posts: 13


My Contributions
In my source code, it has this same long code assigned to 60 buttons:

CODE
intRoomNumber = 1
frmInformation.Hide
frmConfirm.Hide
lblGuestName.Caption = strName(intRoomNumber)
lblCheckIn.Caption = dtmCheckIn(intRoomNumber)
lblCheckOut.Caption = dtmCheckOut(intRoomNumber)
lblID.Visible = True
lblInfo(0).Visible = True
lblGuestName.Visible = True
lblID.Caption = intRoomNumber
If blnTaken(intRoomNumber) = True Then
lblInfo(0).Visible = True
lblInfo(1).Visible = True
lblInfo(2).Visible = True
lblInfo(3).Visible = True
lblTaken.Visible = True
lblCheckIn.Visible = True
lblCheckOut.Visible = True
cmdOK.Enabled = True
cmdDetails.Enabled = True
cmdCheckIn.Enabled = False
cmdCheckOut.Enabled = True
cmdReserve.Enabled = False
cmdRevert.Enabled = False
ElseIf blnReserve(intRoomNumber) = True Then
cmdOK.Enabled = True
cmdDetails.Enabled = False
cmdCheckIn.Enabled = False
cmdCheckOut.Enabled = False
cmdReserve.Enabled = False
cmdRevert.Enabled = True
lblReserved.Visible = True
Else
lblVacant.Visible = True
cmdOK.Enabled = True
cmdDetails.Enabled = False
cmdCheckIn.Enabled = True
cmdCheckOut.Enabled = False
cmdReserve.Enabled = True
cmdRevert.Enabled = False
End If


It was troubling to have it pasted 60 times in the same button plus its gonna cost me more for printing so I need to shorten this up. Not removing unnecessary strings in the code but its usage, meaning that I don't have to paste the same code for 60 times.

Is there a way I can use to simplify the code and solve my problem?
I was thinking if I can put this one in a Module and then call the entire code out in any form whenever I wanted.

User is offlineProfile CardPM
+Quote Post


birdflu9999

RE: Reducing The Use Of Repetitive Codes

27 Jun, 2009 - 06:46 PM
Post #2

New D.I.C Head
Group Icon

Joined: 26 Jun, 2009
Posts: 22



Thanked: 2 times
Dream Kudos: 25
My Contributions
QUOTE(Kinnison @ 27 Jun, 2009 - 04:09 PM) *

In my source code, it has this same long code assigned to 60 buttons:

CODE
intRoomNumber = 1
frmInformation.Hide
frmConfirm.Hide
lblGuestName.Caption = strName(intRoomNumber)
lblCheckIn.Caption = dtmCheckIn(intRoomNumber)
lblCheckOut.Caption = dtmCheckOut(intRoomNumber)
lblID.Visible = True
lblInfo(0).Visible = True
lblGuestName.Visible = True
lblID.Caption = intRoomNumber
If blnTaken(intRoomNumber) = True Then
lblInfo(0).Visible = True
lblInfo(1).Visible = True
lblInfo(2).Visible = True
lblInfo(3).Visible = True
lblTaken.Visible = True
lblCheckIn.Visible = True
lblCheckOut.Visible = True
cmdOK.Enabled = True
cmdDetails.Enabled = True
cmdCheckIn.Enabled = False
cmdCheckOut.Enabled = True
cmdReserve.Enabled = False
cmdRevert.Enabled = False
ElseIf blnReserve(intRoomNumber) = True Then
cmdOK.Enabled = True
cmdDetails.Enabled = False
cmdCheckIn.Enabled = False
cmdCheckOut.Enabled = False
cmdReserve.Enabled = False
cmdRevert.Enabled = True
lblReserved.Visible = True
Else
lblVacant.Visible = True
cmdOK.Enabled = True
cmdDetails.Enabled = False
cmdCheckIn.Enabled = True
cmdCheckOut.Enabled = False
cmdReserve.Enabled = True
cmdRevert.Enabled = False
End If


It was troubling to have it pasted 60 times in the same button plus its gonna cost me more for printing so I need to shorten this up. Not removing unnecessary strings in the code but its usage, meaning that I don't have to paste the same code for 60 times.

Is there a way I can use to simplify the code and solve my problem?
I was thinking if I can put this one in a Module and then call the entire code out in any form whenever I wanted.



Hi K...

I think that if you put it in a module it will only result in an error why not use a function with parameters

exmaple of the function:
Function name is Toggle..
CODE

Function Toggle(btnOK As Boolean, btndetails As Boolean, btnCheckin As Boolean, btnCheckout As Boolean, btnReserve As Boolean, btnRevert As Boolean)
cmdOK.Enabled = btnOK
cmdDetails.Enabled = btndetails
cmdCheckIn.Enabled = btnCheckin
cmdCheckOut.Enabled = btnCheckout
cmdReserve.Enabled = btnReserve
cmdRevert.Enabled = btnRevert
End Function


So if you use the function to your code it will look like this:
CODE

frmInformation.Hide
frmConfirm.Hide
lblGuestName.Caption = strName(intRoomNumber)
lblCheckIn.Caption = dtmCheckIn(intRoomNumber)
lblCheckOut.Caption = dtmCheckOut(intRoomNumber)
lblID.Visible = True
lblInfo(0).Visible = True
lblGuestName.Visible = True
lblID.Caption = intRoomNumber

If blnTaken(intRoomNumber) = True Then
lblInfo(0).Visible = True
lblInfo(1).Visible = True
lblInfo(2).Visible = True
lblInfo(3).Visible = True
lblTaken.Visible = True
lblCheckIn.Visible = True
lblCheckOut.Visible = True

Toggle True,True,False,True,False,False

ElseIf blnReserve(intRoomNumber) = True Then

lblReserved.Visible = True
Toggle True,False,False,False,False,True

Else

lblVacant.Visible = True
Toggle True,False,True,False,True,False

End If


and don't forget to include the function in to your form
hope this will help you...

User is offlineProfile CardPM
+Quote Post

Kinnison

RE: Reducing The Use Of Repetitive Codes

27 Jun, 2009 - 10:59 PM
Post #3

New D.I.C Head
*

Joined: 24 Jun, 2009
Posts: 13


My Contributions
Ah, this really helps alot in cutting down the coding.

Thanks alot for the help smile.gif
User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic

Time is now: 11/8/09 12:42AM

Live VB Help!

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter Fan Us On Facebook

VB Tutorials

Reference Sheets

VB Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month