PDA

View Full Version : Checkbox problem



Cam
08-30-2009, 12:09 AM
I am creating a userform, from which the user has to enter an amount value in 5 seperate textboxes and am wanting to add 5 checkboxes for another reason. However, I am not very good at programming, I was hoping someone here could help me.
Below is the code to make the form work thus far.

Option Explicit
Dim lCurrentRow As Long
Dim r As Long
Dim lSb As Long
Private Sub CheckBox1_Click()
End Sub
Private Sub UserForm_Initialize()
lSb = ActiveSheet.UsedRange.Rows.Count
With Me
.ScrollBar1.Min = 1 'assume header row in Row 1
.ScrollBar1.Max = lSb
.ScrollBar1.Value = .ScrollBar1.Min
End With
End Sub
Private Sub cmdPrev_Click()
' Show previous only if not already in first row:
If lCurrentRow > 1 Then
' Save form contents before changing rows:
SaveRow
' Decrement row number:
lCurrentRow = lCurrentRow - 1
' Show contents of row in the form:
LoadRow
End If
End Sub
Private Sub cmdNext_Click()
' Save form contents before changing rows:
SaveRow
' Increment row number:
lCurrentRow = lCurrentRow + 1
' Show contents of row in the form:
LoadRow
End Sub
Private Sub cmdAdd_Click()
' Save form contents before changing rows:
SaveRow

' Set current row to first empty row, i.e. one row after
' the last row currently in use:
If Not IsNumeric(Me.TextBox2.Value) Then
MsgBox "The Amount box must contain a number.", vbExclamation, "Staff Expenses"
Me.TextBox2.Value = ""
Me.lblFirstDate.Caption = ""
Cells(lCurrentRow, 4).Value = ""
Cells(lCurrentRow, 5).Value = ""
Me.TextBox2.SetFocus
Exit Sub
End If
If Cells(1, 1).Value = "" Then
lCurrentRow = lCurrentRow ' (list is empty - start in row 2)
Else
lCurrentRow = ActiveSheet.UsedRange.Rows.Count + 1
End If
LoadRow
SaveRow
UserForm_Activate
End Sub
Private Sub UserForm_Activate()
' Read initial values from Row 2:
lCurrentRow = 2
LoadRow
CheckBox1 = False
End Sub
Private Sub cmdClose_Click()
' Save form contents before closing:
Unload Me ' Close the form
IndexForm.Show
End Sub
Private Sub LoadRow()
lblCode.Caption = Cells(lCurrentRow, 1).Value
lblProdName.Caption = Cells(lCurrentRow, 2).Value
lblDestination.Caption = Cells(lCurrentRow, 3).Value
TextBox2.Text = Cells(lCurrentRow, 4).Value
lblFirstDate.Caption = Cells(lCurrentRow, 5).Value
TextBox4.Text = Cells(lCurrentRow, 6).Value
lblSecondDate.Caption = Cells(lCurrentRow, 7).Value
TextBox6.Text = Cells(lCurrentRow, 8).Value
lblThirdDate.Caption = Cells(lCurrentRow, 9).Value
TextBox8.Text = Cells(lCurrentRow, 10).Value
lblFourthDate.Caption = Cells(lCurrentRow, 11).Value
TextBox10.Text = Cells(lCurrentRow, 12).Value
lblFifthDate.Caption = Cells(lCurrentRow, 13).Value
CheckBox1.Value = Cells(lCurrentRow, 14).Value
End Sub
Private Sub SaveRow()
Cells(lCurrentRow, 1).Value = lblCode.Caption
Cells(lCurrentRow, 2).Value = lblProdName.Caption
Cells(lCurrentRow, 3).Value = lblDestination.Caption
Cells(lCurrentRow, 4).Value = TextBox2.Text
Cells(lCurrentRow, 5).Value = lblFirstDate.Caption
Cells(lCurrentRow, 6).Value = TextBox4.Text
Cells(lCurrentRow, 7).Value = lblSecondDate.Caption
Cells(lCurrentRow, 8).Value = TextBox6.Text
Cells(lCurrentRow, 9).Value = lblThirdDate.Caption
Cells(lCurrentRow, 10).Value = TextBox8.Text
Cells(lCurrentRow, 11).Value = lblFourthDate.Caption
Cells(lCurrentRow, 12).Value = TextBox10.Text
Cells(lCurrentRow, 13).Value = lblFifthDate.Caption
Cells(lCurrentRow, 14).Value = CheckBox1.Value
End Sub
Private Sub ScrollBar1_Change()
lCurrentRow = Me.ScrollBar1.Value + 1
Me.lblCode.Caption = Cells(lCurrentRow, 1).Value
Me.lblProdName.Caption = Cells(lCurrentRow, 2).Value
Me.lblDestination.Caption = Cells(lCurrentRow, 3).Value
Me.TextBox2.Value = Cells(lCurrentRow, 4).Value
Me.lblFirstDate.Caption = Cells(lCurrentRow, 5).Value
Me.TextBox4.Value = Cells(lCurrentRow, 6).Value
Me.lblSecondDate.Caption = Cells(lCurrentRow, 7).Value
Me.TextBox6.Value = Cells(lCurrentRow, 8).Value
Me.lblThirdDate.Caption = Cells(lCurrentRow, 9).Value
Me.TextBox8.Value = Cells(lCurrentRow, 10).Value
Me.lblFourthDate.Caption = Cells(lCurrentRow, 11).Value
Me.TextBox10.Value = Cells(lCurrentRow, 12).Value
Me.lblFifthDate.Caption = Cells(lCurrentRow, 13).Value
Me.CheckBox1.Value = Cells(lCurrentRow, 14).Value
End Sub
Private Sub CheckBox1_Change()
If CheckBox1 = True Then
chkVegetarian.Enabled = True
Else
chkVegetarian.Enabled = False
chkVegetarian = False
End If
End Sub
Private Sub TextBox2_Change()
lblFirstDate = Date
End Sub
Private Sub TextBox4_Change()
lblSecondDate = Date
End Sub
Private Sub TextBox6_Change()
lblThirdDate = Date
End Sub
Private Sub TextBox8_Change()
lblFourthDate = Date
End Sub
Private Sub TextBox10_Change()
lblFifthDate = Date
End Sub


I have tried looking for the appropriate code, but to no avail.

Aussiebear
08-30-2009, 12:20 AM
Hi Cam, Welcome to the VBA Express forums. On the form that you indicated in your post, what purpose do the check boxes relate to?