0% found this document useful (0 votes)
14 views

New Text Document

The provided VBA code is a subroutine for a button click event that validates user input for a date and branch name before saving the data to a worksheet. It checks for empty fields, validates the date format, and ensures the branch name does not contain numbers, highlighting errors as needed. Upon successful validation, it increments the diary number and saves the new entry in the 'Requisition Entries' sheet, displaying a success message with the new diary number.

Uploaded by

f60simple
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views

New Text Document

The provided VBA code is a subroutine for a button click event that validates user input for a date and branch name before saving the data to a worksheet. It checks for empty fields, validates the date format, and ensures the branch name does not contain numbers, highlighting errors as needed. Upon successful validation, it increments the diary number and saves the new entry in the 'Requisition Entries' sheet, displaying a success message with the new diary number.

Uploaded by

f60simple
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 2

Private Sub cmbSubmit_Click()

Dim ws As Worksheet
Dim lastRow As Long
Dim newDiaryNo As Long
Dim lastDiaryNo As Variant

' ? Reset field colors before validation


Textdate.BackColor = vbWhite
TextBranch.BackColor = vbWhite

' ? Check if fields are empty


If Trim(Textdate.Value) = "" Then
Textdate.BackColor = vbYellow ' ?? Highlight empty field
MsgBox "Please enter a valid date!", vbExclamation, "Input Error"
Exit Sub
End If

If Trim(TextBranch.Value) = "" Then


TextBranch.BackColor = vbYellow ' ?? Highlight empty field
MsgBox "Please enter a valid branch name!", vbExclamation, "Input Error"
Exit Sub
End If

' ? Validate Date Field (TextDate)


If Not IsDate(Textdate.Value) Then
Textdate.BackColor = vbRed ' ?? Highlight field with error
MsgBox "Invalid date format! Please enter a valid date.", vbCritical, "Date
Error"
Exit Sub
End If

' ? Validate Branch Field (TextBranch - Should Not Contain Digits)


If TextBranch.Value Like "*[0-9]*" Then
TextBranch.BackColor = vbRed ' ?? Highlight field with error
MsgBox "Branch name should not contain numbers!", vbCritical, "Branch
Error"
Exit Sub
End If

' ? Set the "Requisition Entries" sheet


Set ws = ThisWorkbook.Sheets("Requisition Entries")

' ? Find the last row in column A


lastRow = ws.Cells(ws.Rows.Count, "A").End(xlUp).Row

' ? Check if the last row contains a valid number


lastDiaryNo = ws.Cells(lastRow, 1).Value

' ?? Diary No. will always start from 1 if no previous records exist
If IsNumeric(lastDiaryNo) And lastRow > 1 Then
newDiaryNo = CLng(lastDiaryNo) + 1 ' Convert to Long before incrementing
Else
newDiaryNo = 1 ' ?? Start from 1 instead of 1001
End If

' ? Update the Diary No. label with "Diary No. XXXX"
lbldiaryno.Caption = "Diary No. " & newDiaryNo

' ? Insert a new entry


ws.Cells(lastRow + 1, 1).Value = newDiaryNo ' Diary No.
ws.Cells(lastRow + 1, 2).Value = Textdate.Value ' Date
ws.Cells(lastRow + 1, 3).Value = TextBranch.Value ' Branch

' ? Clear input fields


Textdate.Value = ""
TextBranch.Value = ""

' ? Success message with Diary No.


MsgBox "Record has been successfully saved!" & vbNewLine & "Diary No: " &
newDiaryNo, vbInformation, "Success"
End Sub

You might also like