PDA

View Full Version : again more VB help needed


George Hallam
11-04-2008, 10:13 AM
Ok VB seems to be getting harder and harder


i designed the form that looks like the one below (label10 was put there by accident)


http://i289.photobucket.com/albums/ll221/jiggyghallam/enter.jpg


when the from is filled out the user clicks enter then all of the data entered goes into the boxes below. This can be done as many times as is needed and therefor changing the percentages each time

to be honest on this one i am toootally clueless i know it involves a lot of if statements and variables but i don't know what to do i have been staring at it for hours now putting in code here is what i have done so far and its not even 5% complete help :confused: :rolleyes:


Option Explicit


Private Sub cmdsubmit_Click()
Dim has_had_a_holiday_past_12_months As Boolean
Dim holiday_in_UK As Boolean
Dim holiday_abroad As Boolean
Dim less_than_2_weeks As Boolean
Dim more_than_2_weeks As Boolean
Static taken_test As Integer
Static been_on_holiday_for_percentage As Single
Static not_been_on_holiday_for_percentage As Single
Dim stayed_in_uk_for_percentage As Single
Dim went_abroad_for_percentage As Single
Dim went_less_than_2_weeks_for_percentage As Single
Dim went_more_than_2_weeks_for_percentage As Single
taken_test = 0
went_more_than_2_weeks_for_percentage = 0
went_less_than_2_weeks_for_percentage = 0
stayed_in_uk_for_percentage = 0
went_abroad_for_percentage = 0
been_on_holiday_for_percentage = 0
not_been_on_holiday_for_percentage = 0
holiday_in_UK = optuk.Value
holiday_abroad = optabroad.Value
has_had_a_holiday_past_12_months = optyes12months.Value
less_than_2_weeks = optlessthan2weeks.Value
more_than_2_weeks = optmorethan2weekse.Value
If holiday_past_12_yes = True Then
taken_test = taken_test + 1
been_on_holiday_for_percentage = been_on_holiday_for_percentage + 1
Else
taken_test = taken_test + 1
not_been_on_holiday_for_percentage = not_been_on_holiday_for_percentage + 1
End If
End Sub

Private Sub Form_Load()
Form1.Show
Form1.SetFocus
End Sub

Private Sub optno12months_Click()
optuk.Enabled = False
optabroad.Enabled = False
opt2.Enabled = False
opt2more.Enabled = False
End Sub

Private Sub optyes12months_Click()
optuk.Enabled = True
optabroad.Enabled = True
opt2.Enabled = True
opt2more.Enabled = True
End Sub

Paul Komski
11-05-2008, 06:56 AM
You have mainly got three issues to resolve.

The first is where or how to store the data between calls. One can use the registry or text files or databases for storage between each reload of the application but that is much more advanced. For casual use during one load then arrays or static variables are common methods but another is to just use hidden (or visible if you prefer) text boxes. Using arrays is a bit more advanced so we will stay with static variables and/or text boxes for now. You need will need six of them. For example:

Hol_Yes
Hol_No
UK_Yes
UK_No
LongHOl_Yes
LongHol_No

I always find it less confusing to use multiple functions/routines along the lines of the following code which hopefully will be enough for you to proceed.


The second issue is what to do when the command button is clicked. (A) The static variables/text boxes will need updating and recalculations done as necessary and (B) The buttons will need resetting to whatever default values you want them to have.

Option Explicit

'This form starts out with one text box one cmd button and one option button
'Modify as you go along

Private Sub Command1_Click()
UpdateHol
'Create and call the two following routines in similar manner to UpdateHol
'UpdateUKHol
'UpdateLongHol
ResetControls
End Sub

Sub UpdateHol()
Static Hol_Yes As Long, Hol_No As Long
If Me.Option_Holiday Then
Hol_Yes = Hol_Yes + 1
Else
Hol_No = Hol_No + 1
End If
Me.Text1 = 100 * Hol_Yes / (Hol_Yes + Hol_No)
End Sub

Sub ResetControls()
Me.Option_Holiday = 0 'or whatever other default
' etc for all controls
End Sub

Lastly, if you are loading a second form from the main form remember to unload that second form when you unload/close the main form or the application.

George Hallam
11-05-2008, 12:28 PM
Ok i did it all and all works nice


Option Explicit
Dim interviewed As Integer 'declares all the varibles as stated in their names
Dim UKless2weeks As Integer
Dim UKmore2weeks As Integer
Dim abroadless2weeks As Integer
Dim abroadmore2weeks As Integer
Dim noholiday As Integer

Private Sub cmdenter_Click()
If optyes.Value = True Then
interviewed = interviewed + 1 'adds one to the interveiwed varible saying that someone has been interviewed
If optuk.Value = True And optless2weeks.Value = True Then
UKless2weeks = UKless2weeks + 1 'adds one to the UKless2weeks varible saying that someone has been to the UK for less than two weeks
ElseIf optuk.Value = True And optmore2weeks.Value = True Then
UKmore2weeks = UKmore2weeks + 1 'adds one to the UKmore2weeks varible saying that someone has been to the UK for more than two weeks
End If
If optabroad.Value = True And optless2weeks.Value = True Then
abroadless2weeks = abroadless2weeks + 1 'adds one to the abroadless2weeks varible saying that someone has been abroad for less than two weeks
ElseIf optabroad.Value = True And optmore2weeks.Value = True Then
abroadmore2weeks = abroadmore2weeks + 1 'adds one to the abroadmore2weeks varible saying that someone has been abroad for more than two weeks
End If
Else
noholiday = noholiday + 1 'adds one to the stated varibles
interviewed = interviewed + 1
End If
If interviewed > 0 Then
lblnoholiday = noholiday / interviewed * 100 'works out percentages of the stated varibles
lblukless2weeks = UKless2weeks / interviewed * 100
lblukmore2weeks = UKmore2weeks / interviewed * 100
lblabroadless2weeks = abroadless2weeks / interviewed * 100
lblabroadmore2weeks = abroadmore2weeks / interviewed * 100
End If
End Sub

Private Sub Form_Load()
'upon form load it sets all of the varibles to a value of zero
Form1.Show
Form1.SetFocus
interviewed = 0
UKless2weeks = 0
UKmore2weeks = 0
abroadless2weeks = 0
abroadmore2weeks = 0
noholiday = 0
End Sub

Private Sub optno_Click()
'When the optno button is selected this makes all of the other option boxes below it both "greyed out" and sets all of their values to one so that it does
'not effect the results
optuk.Enabled = False
optabroad.Enabled = False
optless2weeks.Enabled = False
optmore2weeks.Enabled = False
optuk.Value = False
optabroad.Value = False
optless2weeks.Value = False
optmore2weeks.Value = False
End Sub

Private Sub optyes_Click()
'This enables all of the option boxes incase someone clicks no then yes
optuk.Enabled = True
optabroad.Enabled = True
optless2weeks.Enabled = True
optmore2weeks.Enabled = True
End Sub



thank you sooo much for all the help again Paul :D