how I validating this - vba

Private Sub Command261_Click()
If IsNull(CompanyName.Value Or ID.Value Or Date_Received.Value Or Clerk_Revceived) = "" Then
MsgBox "Please Complete Company Name"
cancel = False
Else
DoCmd.Close
End If
End Sub

Probably something like this:
Private Sub Command261_Click()
If IsNull(CompanyName.Value + ID.Value + Date_Received.Value + Clerk_Revceived.Value) Then
MsgBox "Please Complete Company Info"
' Cancel = True ' Nothing to cancel here.
Else
DoCmd.Close
End If
End Sub

Related

Message box for null fields

I am trying to create a database for part locations. Three of the fields (JobNumber, PartNumber and Location) are required fields and I have an If statement written to check for Nulls and error handling to give a message box. After the message box is closed, it will let me return to the form but it will not allow me to edit the field again AND it still adds the incomplete data to the table. Any advice?
Private Sub cmdAddNew_Click()
If IsNull(Me.JobNumber) Then
GoTo cmdAddNew_Click_Err
ElseIf IsNull(Me.PartNumber) Then
GoTo cmdAddNew_Click_Err
ElseIf IsNull(Me.Location) Then
GoTo cmdAddNew_Click_Err
End If
On Error GoTo cmdAddNew_Click_Err
On Error Resume Next
DoCmd.GoToRecord , "", acNewRec
If (MacroError <> 0) Then
Beep
MsgBox MacroError.Description, vbOKOnly, ""
End If
cmdAddNew_Click_Exit:
Exit Sub
cmdAddNew_Click_Err:
MsgBox "Job Number, Part Number and Location are required."
End Sub
Adding Cancel = True in your second if statement should fix this.
Private Sub cmdAddNew_Click()
If IsNull(Me.JobNumber) Then
GoTo cmdAddNew_Click_Err
ElseIf IsNull(Me.PartNumber) Then
GoTo cmdAddNew_Click_Err
ElseIf IsNull(Me.Location) Then
GoTo cmdAddNew_Click_Err
End If
On Error GoTo cmdAddNew_Click_Err
On Error Resume Next
DoCmd.GoToRecord , "", acNewRec
If (MacroError <> 0) Then
Beep
MsgBox MacroError.Description, vbOKOnly, ""
Cancel = True
End If
cmdAddNew_Click_Exit:
Exit Sub
cmdAddNew_Click_Err:
MsgBox "Job Number, Part Number and Location are required."
End Sub
Alternatively, you could define functions to check for a null value and return a boolean result:
Function isvalid(Field as string) As Boolean
If IsNull(field)
isvalid = False
Else
isvalid = True
End If
End Function
Sub Check_valid()
Call isvalid(field 1)
Call isvalid(f2)
Call isvalid(f3)
If isvalid(field 1) = false Or isvalid(f2) = false Or isvalid(f3) = false Then
msgbox "Job Number, Part Number and Location are required."
Exit Sub
End If
End Sub
Hope this helps!

How to implement a password in a button

I have a button to lock unlock a form with a subform. To enable the button I would like to set a password with a msgbox. But I have an issue in my code.
I first use the code to show the password msgbox with conditional, if the password is right the user locks/unlocks the forms. But I have end with statement in wrong place.
Private Sub bloquear_Click()
With Me.bloquear
Dim strPasswd
strPasswd = InputBox("Enter Password", "Restricted Form")
If strPasswd = "" Or strPasswd = Empty Then
MsgBox "No Input Provided", vbInformation, "Required Data"
Exit Sub
End If
If strPasswd = "Password" Then
If .Caption = "Unlock" Then
Me.AllowAdditions = True
Me.AllowEdits = True
Me.CONSULTA_PRODUCTOS.Form.AllowAdditions = True
Me.CONSULTA_PRODUCTOS.Form.AllowEdits = True
.Caption = "Lock"
Else
Me.AllowAdditions = False
Me.AllowEdits = False
Me.CONSULTA_PRODUCTOS.Form.AllowAdditions = False
Me.CONSULTA_PRODUCTOS.Form.AllowEdits = False
.Caption = "Unlock"
Me.Refresh
End If
End With
Else
MsgBox "Sorry, you do not have access to this form", _
vbOKOnly, "Important Information"
Exit Sub
End If
End Sub
The msgbox shows
Compile error, end with without with
I don't know what is wrong because the with is there.
Thanks

Made null textbox and set focus on another

I'm using code below, for check another textbox value when exiting initial textbox and if it null, making initial one null and set focus on final textbox.
But i give this error: Run-time error'-2147467259(80004005)': Unspecific error.
when i made comment this line (txtTimeUnit = vbNullString), macro code works correctly.
whats the problem of that line's command and please help me correcting code.
Private Sub txtTimeUnit_Exit(ByVal Cancel As MSForms.ReturnBoolean)
If txtStartDate.Text = vbNullString Then
txtTimeUnit = vbNullString
txtStartDate.SetFocus
Exit Sub
End If
End Sub
Like I said your code works. Here is an example
Private Sub txtTimeUnit_Exit(ByVal Cancel As MSForms.ReturnBoolean)
If txtStartDate.Text = vbNullString Then
txtTimeUnit.Text = vbNullString
txtStartDate.SetFocus
Exit Sub
End If
End Sub
The only way it will not work is when there is another piece of code which is setting the Cancel = True. For example
Private Sub txtTimeUnit_Exit(ByVal Cancel As MSForms.ReturnBoolean)
If IsError(Application.Match(txtTimeUnit.Text, Range("intTable[Time Unit]"), 0)) Then
Cancel = True
End If
If txtStartDate.Text = vbNullString Then
txtTimeUnit.Text = vbNullString
txtStartDate.SetFocus
Exit Sub
End If
End Sub
To prevent such kind of errors you can use a Boolean Variable
Dim boolOnce As Boolean
Private Sub txtTimeUnit_Exit(ByVal Cancel As MSForms.ReturnBoolean)
If boolOnce = False Then
boolOnce = True
If IsError(Application.Match(txtTimeUnit.Text, Range("intTable[Time Unit]"), 0)) Then
Cancel = True
End If
Else
boolOnce = False
End If
If txtStartDate.Text = vbNullString Then
txtTimeUnit.Text = vbNullString
txtStartDate.SetFocus
Exit Sub
End If
End Sub

Error message box select statement to handle yes or no

need help with the yes no portion of the error message, if yes I want the code to be launched again and if no then exit sub.
Public Sub Reset()
Dim pt As PivotTable
Dim slice As Slicer
Application.ScreenUpdating = False
ActiveWorkbook.Model.Refresh
For Each pt In ActiveSheet.PivotTables
pt.RefreshTable
For Each slice In pt.Slicers
slice.SlicerCache.ClearAllFilters
On Error GoTo 0
Next slice
pt.PivotCache.Refresh
Next pt
Error 0:
MsgBox "Sorry, Missing data, do you wish to continue?", _
vbCritical vbYesNo, "Restart process!"
Select Case vbYesNo
Case yes
MergeMultipleSheets
Case Else
Exit Sub
End Select
Application.ScreenUpdating = True
End Sub
Here is yes no portion of your code (assuming that you want to launch module MergeMultipleSheets if yes button is clicked):
Sub Reset()
Dim xlAns As Integer
xlAns = MsgBox("Sorry, Missing data, do you wish to continue?", vbYesNo, "Restart process!")
Select Case xlAns
Case vbYes
' do something
' if You want to call sub MergeMultipleSheets
MergeMultipleSheets
Case Else
' do something
Exit Sub
End Select
End Sub
Quick Example
Sub MsgBx()
Dim xlMsgBox As Integer
Dim Cancel As Boolean
xlMsgBox = MsgBox("Do you want to do this ", vbYesNoCancel)
If xlMsgBox = vbCancel Then
Cancel = True ' Exit
Exit Sub
ElseIf xlMsgBox = vbYes Then
' do something
ElseIf xlMsgBox = vbNo Then
' do something
End If
End Sub

Vba Button Code error

Hello I am having an error with this code at the Line Else I want it to change the caption if a name has been entered if not then exit
Private Sub cmdButton_Click()
Dim name As String
With cmdButton
If .Caption = "CommandButton1" Then
name = InputBox("Enter Template Name.")
End If
If StrPtr(name) = 0& Then Exit Sub
Else
.Caption = name
End If
End With
End Sub
If Len(name) = 0 Then 'exit sub if user doesn't enter a name (or cancels)
Exit Sub
Else
.Caption = name
End If