MS Access using function on onclick event of button - ms-access

I have below function on form:
Function StringsHaveAMatchingCharacter(String1 As Variant, String2 As Variant) As Boolean
' Return True if the two strings have at least one character in common;
' otherwise return False.
Dim lngX As Long
If IsNull(String1) Or IsNull(String2) Then
' No point checking for a match.
Else
For lngX = 1 To Len(String1)
If InStr(String2, Mid$(String1, lngX, 1)) <> 0 Then
StringsHaveAMatchingCharacter = True
Exit Function
End If
Next lngX
End If
StringsHaveAMatchingCharacter = False
End Function
I can call this function with commandbutton on form. But how to use specify on my event if function returns "false" or "true" as I want new dialog form to be open if function returns "True" else I want msgbox and cancel opening dialog form.

Not sure I understand the problem. You simply call the function and use its return value.
Private Sub cmdOpenDialog_Click()
Dim isMatch As Boolean
isMatch = StringsHaveAMatchingCharacter(Me!Text1.Value, Me!Text2.Value)
If isMatch Then
DoCmd.OpenForm "myDialog"
Else
MsgBox "Nope."
End If
End Sub

Related

Making a form read-only for a certain user type

I have created a login form with a combo box for the user type (Admin, User) and a text box for the password. The code for the form is as follows.
Private Sub txtPassword_AfterUpdate()
If IsNull(Me.cboUser) Then
MsgBox "You need to select a user!", vbCritical
Me.cboUser.SetFocus
Else
If Me.txtPassword = Me.cboUser.Column(2) Then
If Me.cboUser.Column(3) = True Then
MsgBox "Password does not match, please re-enter!", vboOkOnly
Me.txtPassword = Null
Me.txtPassword.SetFocus
End If
DoCmd.OpenForm "FE1"
Me.Visible = False
Else
MsgBox "Password does not match, please re-enter!", vboOkOnly
Me.txtPassword = Null
Me.txtPassword.SetFocus
End If
End If
End Sub
Private Sub cboUser_AfterUpdate()
Forms!frmLogin!cboUser.Column (2)
End Sub
If the log in is as a User, when they get to the FE1 form, I want them just to be able to read the form, and not make any changes. The code I've been trying to use for this is as follows:
Private Sub Form_Open()
If Forms!frmLogin!cboUser.Column(2) = 2 Then
Me.AllowEdits = False
Me.AllowAdditions = False
Me.AllowDeletes = False
Else
Me.AllowEdits = True
Me.AllowAdditions = True
Me.AllowDeletes = True
End If
End Sub
But I keep getting the error:
The expression On Open you entered as the event property setting
produced the following error: Procedure declaration does not
match description of event or procedure having the same name.
*The expression may not result in the name of a macro, the name of a user-defined function, or [Event Procedure].
*There may have been an error evaluating the function, event, or macro.
It's possible I've just been looking at this for too long, but I can't figure out where I've gone wrong!?
Your Form_Open procedure has the wrong signature, missing the Cancel parameter.
It must be:
Private Sub Form_Open(Cancel As Integer)
Don't write event procedures by hand, let Access create them.
Edit
I suggest you completely remove the Form_Open sub. Then let Access create it from the property sheet.
And you can simplify your code by using a variable like this:
Private Sub Form_Open(Cancel As Integer)
Dim AllowWriting As Boolean
AllowWriting = Not (Forms!frmLogin!cboUser.Column(2) = 2)
Me.AllowEdits = AllowWriting
Me.AllowAdditions = AllowWriting
Me.AllowDeletes = AllowWriting
End Sub
or even shorter with the RecordsetType Property:
Private Sub Form_Open(Cancel As Integer)
If Forms!frmLogin!cboUser.Column(2) = 2 Then
Me.RecordsetType = 2 ' Snapshot = read-only
Else
Me.RecordsetType = 0 ' Dynaset = read-write
End If
End Sub

Microsoft Access: Saving combo box data from form to table

I have a table that stores data entered by a user in a form. All of the data entered in the form saves properly in the table except for those fields chosen from a combo box. I have checked to make sure that all of these combo box selections in the Form Design View are bound to the associated table fields in the underlying data.
Currently, I have the data type for those fields in the design view of the table set as "Short Text". I am wondering if the problem is that I need to set the data type to a different type, or if there is something else causing this problem.
Option Compare Database
Option Explicit
Dim stay As String
Function EnableInfo()
Me.ADescription.Locked = False
Me.ComboVendor.Locked = False
Me.ComboVendor.Locked = False
Me.onHand.Locked = False
Me.onOrder.Locked = False
Me.CostB.Locked = False
Me.ListPriceB.Locked = False
End Function
Function DisableInfo()
Me.ADescription.Locked = True
Me.ComboVendor.Locked = True
Me.onHand.Locked = True
Me.onOrder.Locked = True
Me.CostB.Locked = True
Me.ListPriceB.Locked = True
End Function
'------------------------------------------------------------
' Function that disables/enable when command buttons when ADD and Edit are clicked!
'------------------------------------------------------------
Function AddEdit()
Me.CmdAdd.Enabled = False
Me.CmdEdit.Enabled = False
Me.CmdExit.Enabled = False
Me.CmdSave.Enabled = True
Me.CmdCancel.Enabled = True
Me.AllowAdditions = True
Me.AllowDeletions = True
Me.AllowEdits = True
Call EnableInfo
End Function
'------------------------------------------------------------
' Function that disables/enable when command buttons when Save and Cancel are clicked!
'------------------------------------------------------------
Function SaveCancel()
Me.CmdAdd.Enabled = True
Me.CmdEdit.Enabled = True
Me.CmdExit.Enabled = True
Me.CmdSave.Enabled = False
Me.CmdCancel.Enabled = False
Call DisableInfo
End Function
'------------------------------------------------------------
' Function that enables navigation buttons
'------------------------------------------------------------
Function EnableNavigation()
Me.cmdFirst.Enabled = True
Me.cmdNext.Enabled = True
Me.cmdPrevious.Enabled = True
Me.cmdlast.Enabled = True
End Function
'------------------------------------------------------------
' Function that disables navigation buttons
'------------------------------------------------------------
Function DisableNavigation()
Me.cmdFirst.Enabled = False
Me.cmdNext.Enabled = False
Me.cmdPrevious.Enabled = False
Me.cmdlast.Enabled = False
End Function
'------------------------------------------------------------
' Function when the ADD button is clicked
'------------------------------------------------------------
Private Sub CmdAdd_Click()
PartIDtext.SetFocus
stay = PartIDtext.Value
Me.DataEntry = True
Call EnableInfo
Call AddEdit
Call DisableNavigation
End Sub
'------------------------------------------------------------
' Function when the CANCEL button is clicked
'------------------------------------------------------------
Private Sub CmdCancel_Click()
Call SaveCancel
Call DisableInfo
Call EnableNavigation
Me.Undo
Me.DataEntry = False
Me.RecordsetClone.FindFirst "partID = " & stay
Me.Bookmark = Me.RecordsetClone.Bookmark
End Sub
'------------------------------------------------------------
' Function when the EDIT button is clicked
'------------------------------------------------------------
Private Sub CmdEdit_Click()
Call AddEdit
Call EnableInfo
Call DisableNavigation
PartIDtext.SetFocus
stay = PartIDtext.Value
End Sub
'------------------------------------------------------------
' Function when the EXIT button is clicked
'------------------------------------------------------------
Private Sub CmdExit_Click()
DoCmd.Close
End Sub
'------------------------------------------------------------
' Function when the SAVE button is clicked
'------------------------------------------------------------
Private Sub CmdSave_Click()
ADescription = Trim(ADescription.Value)
stay = Me.PartIDtext.Value
If IsNull(Me.ADescription) Or Len(Me.ADescription) < 5 Then
MsgBox "Please enter a description of at least 5 characters"
Me.ADescription.SetFocus
ElseIf IsNull(Me.onHand) Or (Me.onHand) < 0 Then
MsgBox "On hand must have a value greater than 0"
Me.onHand.SetFocus
ElseIf IsNull(Me.ComboVendor) Then
MsgBox "select one"
Me.onHand.SetFocus
ElseIf IsNull(Me.onOrder) Or (Me.onOrder) < 0 Then
MsgBox "On order must have a value greater than 0"
Me.onOrder.SetFocus
ElseIf IsNull(Me.CostB) Or (Me.CostB) < 0 Then
MsgBox "Cost must have a value greater than 0"
Me.CostB.SetFocus
ElseIf IsNull(Me.ListPriceB) Or (Me.ListPriceB) < (Me.CostB) Then
MsgBox "List price must be greater than cost!"
Me.ListPriceB.SetFocus
Else
Me.DataEntry = False
Me.RecordsetClone.FindFirst "partID = " & stay
Me.Bookmark = Me.RecordsetClone.Bookmark
Call SaveCancel
Call DisableInfo
Call EnableNavigation
End If
End Sub
'------------------------------------------------------------
' CmdNext
'------------------------------------------------------------
Private Sub CmdNext_Click()
On Error Resume Next
DoCmd.GoToRecord , "", acNext
End Sub
'------------------------------------------------------------
' CmdPrevious
'------------------------------------------------------------
Private Sub CmdPrevious_Click()
On Error Resume Next
DoCmd.GoToRecord , "", acPrevious
End Sub
'------------------------------------------------------------
' CmdFirst
'------------------------------------------------------------
Private Sub CmdFirst_Click()
DoCmd.GoToRecord , "", acFirst
End Sub
'------------------------------------------------------------
' CmdLast
'------------------------------------------------------------
Private Sub CmdLast_Click()
DoCmd.GoToRecord , "", acLast
End Sub

The IF....then statement in Access VBA

I am testing my understanding on the if then statement, I wrote a little thing down below but when I hit run, nothing happened. I was expecting a msgbox will appear asking me if I want to quit or not and giving me choices to choose. Did I miss anything please. Thanks
Sub testifthenelse(bQuit As Boolean)
Dim s As String
s = "Do you want to quit?"
If MsgBox(s, vbYesNo, "Quite?") = vbYes Then
bQuit = True
Else
bQuit = False
End If
End Sub
you must call it from another sub:
Sub main()
Dim bQuit As Boolean
testifthenelse bQuit
End Sub
while, if you want to run and test it "by itself" then make the argument optional
Sub testifthenelse(Optional bQuit As Variant)
Dim s As String
s = "Do you want to quit?"
If MsgBox(s, vbYesNo, "Quite?") = vbYes Then
bQuit = True
Else
bQuit = False
End If
End Sub

MS Access Query : Check if form is open / Check if parameter requires input

I'm currently trying, in a MS Access Query (using the GUI tool, and not the SQL tool), to check if a specific form is open or not, without using VBA.
Using the following expression :
Expr1 : [Formulaires]![Form1].[Visible]
If Form1 is open, the query works fine, but if Form1 is closed, it asks for input, since the Form1 isn't open, and the variable doesn't exist anymore.
Is there a way to check if a specific form is open, or to check if a certain variable requires input ?
You would need a VBA function to discover this but you could call the function from your query and incorporate the result in your formula.
This function will return true if the form is loaded;
Function IsFormLoaded(strForm As String) As Boolean
Dim frm As Form
For Each frm In Forms
If frm.Name = strForm Then
IsFormLoaded = True
Exit Function
End If
Next
End Function
You can then use the function in an IIF statement;
IIF(IsFormLoaded('MyForm'), MyForm.Control.Value, '')
Yes, use a function:
Public Function IsFormLoaded(ByVal strForm As String) As Boolean
Dim frmForm As Form
Dim booLoaded As Boolean
For Each frmForm In Forms
If frmForm.Name = strForm Then
booLoaded = True
Exit For
End If
Next
IsFormLoaded = booLoaded
Set frmForm = Nothing
End Function
I use this:
If IsOpen("FormName") Then
DoCmd.Close acForm, "FormName"
End if
[put this function in a separate module]
Public Function IsOpen(fn As String) As Boolean 'returns True if form is open
If SysCmd(acSysCmdGetObjectState, acForm, fn) <> 0 Then IsOpen = True
End Function

How to avoid name error in ms access formula?

I want to set DefaultValue of a form field to
=IIf(IsNull([Forms]![MyForm]![MySubForm].[Form]![MyField]);"";[Forms]![MyForm]![MySubForm].[Form]![MyField])
When MyForm is closed I want to set the field value to "". Unfortunately I can't find how to test if MyForm is closed or not : I have tried IsNull, IsObject, very Is it always return #Name?
Is this possible ?
I use a function like this to check to see if a form is open:
Function IsLoaded(ByVal strFormName As String) As Boolean
' Returns True if the specified form is open in Form view or Datasheet view.
Const conObjStateClosed = 0
Const conDesignView = 0
If SysCmd(acSysCmdGetObjectState, acForm, strFormName) <> conObjStateClosed Then
If Forms(strFormName).CurrentView <> conDesignView Then
IsLoaded = True
End If
End If
End Function