Hide and Unhide a field based on another field value - ms-access

I have a form in Access 2010. this form included a combo box named "Type" related to a field in a table that named "Type". another combo box that is "Car_1" is related to a field in the same table that named "Car_1"
I want when the "Type" combo box is empty (null) then the combo box Car_1 should be invisible.
I wrote a code but does not work. actually, the "Car_1" combo box is visible always!
the code is:
Private Sub Form_Current()
If Me.Type.Value = "" Then
Me.Car_1.Visible = False
Else
Me.Car_1.Visible = True
End If
End Sub
any advice is appropriate ...

You could reduce it to a one-liner:
Private Sub Form_Current()
Me!Car_1.Visible = Not IsNull(Me!Type.Value)
End Sub

That's because you're not testing if Me.Type.Value is Null, you're testing if it's a zero-length string. Test if it's Null instead:
Private Sub Form_Current()
If IsNull(Me.Type.Value) Then
Me.Car_1.Visible = False
Else
Me.Car_1.Visible = True
End If
End Sub
Or account for both Null and a zero-length string:
Private Sub Form_Current()
If Me.Type.Value & vbNullString = vbNullString Then
Me.Car_1.Visible = False
Else
Me.Car_1.Visible = True
End If
End Sub

Related

Docmd.Findrecord finds in textbox

I'm using the docmd.Findrecord to find a record based on a value the user enter in a textbox. The problem is that Findrecord considers the textbox as a field and just keep finding the string in the field and not the records in the table I tricked the command by deleting the string in the textbox just before the search and replacing it back after the search. How can I fix this without this extra trick ? Here is the code I use:
Private Sub SearchRecord_Click()
Dim strwhat2find As String
If SearchTxt.Value = "" Then
Else
strwhat2find = SearchTxt.Value
SearchTxt.Value = ""
DoCmd.FindRecord strwhat2find, acAnywhere, False, acSearchAll, True, acAll, True
SearchTxt.Value = strwhat2find
End If
End Sub
End Sub

How do I hide horizontal lines on Access when a particular text box is empty?

I have a report with 5 textboxes each separated by a horizontal line.
The first textbox will always be populated but I would like to suppress the viewing/printing of any of the lines after that if the 2nd textbox is empty.
I've tried this...
Public Function Hide_Lines()
Dim ELRep As Report
Set ELRep = Reports("EL_Report")
If ELRep.Name_2 = Null Then
Set ELRep.Line2.Visible = False
Set ELRep.Line3.Visible = False
Set ELRep.Line4.Visible = False
Set ELRep.Line5.Visible = False
End If
End Function
...but I get a 'Method 'Item' of object 'Reports' failed error.
Is my syntax suspect or am I barking up completely the wrong tree?
Many thanks,
Chas
Cannot compare anything to Null, = Null won't work. Review http://allenbrowne.com/casu-12.html.
Probably a function call is unnecessary complication. Use the report Detail Section OnFormat event with:
Private Sub Detail_Format(Cancel As Integer, FormatCount As Integer)
With Me
If IsNull(.Name_2) Then
.Line1.Visible = False
.Line2.Visible = False
.Line3.Visible = False
.Line4.Visible = False
End If
End With
End Sub
or
Private Sub Detail_Format(Cancel As Integer, FormatCount As Integer)
With Me
.Line1.Visible = Not IsNull(.Name_2)
.Line2.Visible = Not IsNull(.Name_2)
.Line3.Visible = Not IsNull(.Name_2)
.Line4.Visible = Not IsNull(.Name_2)
End With
End Sub
or
Private Sub Detail_Format(Cancel As Integer, FormatCount As Integer)
Dim x As Integer
For x = 1 to 4
Me.Controls("Line" & x).Visible = Not IsNull(Me.Name_2)
Next
End Sub
Be aware OnFormat event only triggers in PrintPreview or direct to printer not ReportView.

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

Check if unbound text box is equal to a string

In an access form, I have an unbound text box when contains a formula. The formula returns "Yes" or "No" based on whether the form has been completed (it ensure that all required boxes are filled in).
On the same form, I have a command button which I want to use to filter out completed forms. Basically, it is a button that save "Save Record" and when clicked, the form - if completed - should disappear (i.e. be filtered out).
Below is what I put in, but I don't know VBA.
Private Sub bttn_Save_Click()
Me.Filter = Me.MainFormComplete = Yes
Me.FilterOn = True
End Sub
How do I get check to see if my field (MainFormComplete) is equal to "Yes"?
Not quite sure what you are trying to do, but it could be:
Private Sub bttn_Save_Click()
Me.Filter = "MainFormComplete = 'Yes'"
Me.FilterOn = True
End Sub
or:
Private Sub bttn_Save_Click()
Me.Filter = "MainFormComplete <> 'Yes'"
Me.FilterOn = True
End Sub
or
if Me.MainFormComplete = Yes then
....
end if
the textbox might not be readable if text was simply entered and not saved. read the text:
if Me.MainFormComplete.Text = Yes then 'or "Yes" or True
....
end if

Access VBA - All checkboxes on form have been checked

I am relatively new to Access VBA and have a form that has around 30 checkboxes on it. When saving the form I want to ensure that all checkboxes have been ticked (set to true). The tickboxes have all got names SC1, SC2....SCN Is there a way to loop through each control and see if it has been set to true?
This is what I have tried but it doesnt seem to read the tickbox -
Private Sub Validate_Data(rstTop)
Dim n As Integer
Dim count As Integer
count = 0
For n = 1 To rstTop
If Form.Controls("SC" & n).Value = False Then
count = count + 1
End If
Next
If count <> 0 Then
MsgBox "Not all Questions have been ticked, please tick and add comments", vbInformation, _
"More information Required"
Else
End If
End Sub
Give this a try, it worked for me.
Option Compare Database
Option Explicit
Private Function Validate_Data() As Boolean
Dim ctl As Control
Validate_Data = True 'initialize
For Each ctl In Me.Form.Controls
If ctl.ControlType = acCheckBox Then
If (ctl.Name Like "SC*") And (ctl.Value = False) Then
MsgBox "Not all Questions have been ticked, please tick and add comments", vbInformation, _
"More information Required"
Validate_Data = False 'something isnt checked
Exit Function
End If
End If
Next ctl
End Function
Private Sub cmdGo_Click()
Validate_Data
End Sub