I Need your urgent support . Validation Rule and VBA Code (1 Viewer)

Ossama22

Registered User.
Local time
Today, 16:48
Joined
Aug 31, 2018
Messages
52
Hello ,

I Have a Tabel which there are alot of cells , for Example i have 2 cells which i wanna to make a validation rule between them : Account Number & Clearance Code

i wanna to create a rule if the clearance code = any value , when i try to write any account number it shows a msg that we cannot add account number because there's a value in clearance code . is that possible in Microsoft Access Table ?

Or if any one can give me VBA cod to do that in form view : when i try to write in account number cell while Clearance code is not null , it shows msg box , can any one help ! Urgently


Another ask , i wanna code : if Clearance Code = HSBC then Account number didnt accept number more than 11 digits , if it greater than or less than that shows msg thacc acc number is wrong
Thx for your support
 

Ranman256

Well-known member
Local time
Today, 09:48
Joined
Apr 9, 2015
Messages
4,339
instead of validation rule, put code in the form, so users cannot edit if theres a ClearanceCode:
Code:
sub form_OnCurrent()
   txtAcctNum.locked = not IsNull(txtClearCode)
end sub

user wont be able to write in the acct# if theres a code.
 

The_Doc_Man

Immoderate Moderator
Staff member
Local time
Today, 08:48
Joined
Feb 28, 2001
Messages
27,122
Ossama, use Ranman's method because validation rules generally do not cross fields. That is, the validation rule ONLY names the one field most of the time. For beginners and even for intermediates, a more complex validation rule is not generally allowed (i.e. one that references another field.) Not impossible but very difficult to make happen, particularly where nulls are possible.
 

Ossama22

Registered User.
Local time
Today, 16:48
Joined
Aug 31, 2018
Messages
52
instead of validation rule, put code in the form, so users cannot edit if theres a ClearanceCode:
Code:
sub form_OnCurrent()
   txtAcctNum.locked = not IsNull(txtClearCode)
end sub
Form Design.jpg

Design Veiw.jpg

Code.jpg
user wont be able to write in the acct# if theres a code.


First of all , thx for your support , but let me tell you something , iam still beginner in dealing with access its all based on self study , so i will attach 3 photos for , Form View and Design view also the code , i tried it but it didnt work . so maybe this photos can help you to give me a specific code to use , sorry for being annoying
 

arnelgp

..forever waiting... waiting for jellybean!
Local time
Today, 21:48
Joined
May 7, 2009
Messages
19,227
here is your validation for [Account Number] textbox (Change Event) on Form.
arrange your textboxes so that clerance get inputted first.
Code:
Private Sub Account_Number_Change()
    Static bolHandled As Boolean
    Dim strClearanceCode As String
    Dim strAccountNumber As String
    strClearanceCode = Me![Clearance Code] & ""
    strAccountNumber = Me![Account Number].Text & ""
    If strClearanceCode = "HSBC" Then
        If Len(strAccountNumber) > 11 Then
            MsgBox "Account Number cannot be more than 11 digits."
            SendKeys "{BACKSPACE}"
        End If
        
    Else
        If strClearanceCode <> vbNullString Then
            If bolHandled = False Then
                MsgBox "You cannot add Account Number because there is a value in the Clearance Code."
                SendKeys "{BACKSPACE}"
                bolHandled = True
                
            Else
                bolHandled = False
                
            End If
        End If
    
    End If
End Sub
 
Last edited:

Ossama22

Registered User.
Local time
Today, 16:48
Joined
Aug 31, 2018
Messages
52
here is your validation for [Account Number] textbox (Change Event) on Form.
arrange your textboxes so that clerance get inputted first.
Code:
Private Sub Account_Number_Change()
    Static bolHandled As Boolean
    Dim strClearanceCode As String
    Dim strAccountNumber As String
    strClearanceCode = Me![Clearance Code] & ""
    strAccountNumber = Me![Account Number].Text & ""
    If strClearanceCode = "HSBC" Then
        If Len(strAccountNumber) > 11 Then
            MsgBox "Account Number cannot be more than 11 digits."
            SendKeys "{BACKSPACE}"
        End If
        

    Else
        If strClearanceCode <> vbNullString Then
            If bolHandled = False Then
                MsgBox "You cannot add Account Number because there is a value in the Clearance Code."
                SendKeys "{BACKSPACE}"
                bolHandled = True
                
            Else
                bolHandled = False
                
            End If
        End If
    
    End If
End Sub



Thx for your efforts , i tried that but it seems that smth is wrong as shows attached , it seems that it's a hopeless case , iam sorry for this disturbance Error.jpg

Str ClearanceCode.jpg
 

arnelgp

..forever waiting... waiting for jellybean!
Local time
Today, 21:48
Joined
May 7, 2009
Messages
19,227
try adding space.

Me![Clearance Code]

or me. clearance_code
 

Cronk

Registered User.
Local time
Today, 23:48
Joined
Jul 4, 2013
Messages
2,771
Ossama, check the name of the control - Properties in design mode.
 

arnelgp

..forever waiting... waiting for jellybean!
Local time
Today, 21:48
Joined
May 7, 2009
Messages
19,227
can yiu zip a sample db and uoliaf.
 

arnelgp

..forever waiting... waiting for jellybean!
Local time
Today, 21:48
Joined
May 7, 2009
Messages
19,227
here is a sample. if still not working upload a zip sample.
 

Attachments

  • sample.zip
    28.9 KB · Views: 74

Users who are viewing this thread

Top Bottom