Hiding a TextBox value - ms-access

This is what I thought of.
Private Sub TextBox_Enter()
TextBox.value.Visible = False
End Sub

If you want to use the value again, store it temporarily in the control's Tag property and restore on Exit.
Private Sub TextBox_Enter()
With TextBox
.Tag = .Value
.Value = Null
End With
End Sub
To restore it if the value is Null:
Private Sub TextBox_Exit(Cancel As Integer)
With TextBox
If IsNull(.Value) Then .Value = .Tag
End With
End Sub

Related

Button to turn change AllowEdits to true in Access using VB

I am trying create a button which changes the value of AllowEdits to False and another for true for a subform. I am using the below code. I get a Runtime error 424 each time I run it.
Option Compare Database
Private Sub Toggle_Edit_Click()
Dim strForm As String
strFormName = Me.Name
Call ToggleEdit(Me)
End Sub
and
Option Compare Database
Public strFormName As String
Sub ToggleEdit(myForm As Form)
Call Message
ctrlControl.AllowEdits = True
End Sub
and if you were interested
Sub Message()
MsgBox "Remember not to overwrite incorrect records"
End Sub
Please add Option Explicit at top of your modules!
I think AllowEdits is a Form property, not a Control property.
Option Explicit
Sub ToggleEdit(myForm As Form)
myForm.AllowEdits = Not myForm.AllowEdits
End Sub
If the code is behind the form itself, you can use Me.
Sub ToggleEdit() 'no parameter
Me.AllowEdits = Not Me.AllowEdits
End Sub
If you want to act at control level, use Locked or Enabled properties.

Hide and Unhide a field based on another field value

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

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

onLost focus Event is not working -ms access

This code is not working. Can anyone help me out?
Private Sub txtErrorComment_5_LostFocus()
If IsEmpty(Me.txtErrorComment_5) Then
MsgBox "Eh..! It cannot be Empty."
End If
End Sub
txtErrorComment_5 is probably Null - which evaluates to False when run through IsEmpty() - yes, this is un-intuitive, but whenever you compare to Null, the result is false.
Try the following:
Private Sub txtErrorComment_5_LostFocus()
If IsEmpty(Me.txtErrorComment_5) or IsNull(Me.txtErrorComment_5) Then
MsgBox "Eh..! It cannot be Empty."
End If
End Sub
Or just:
Private Sub txtErrorComment_5_LostFocus()
If IsNull(Me.txtErrorComment_5) Then
MsgBox "Eh..! It cannot be Empty."
End If
End Sub
IsNull can handle Null's
Edit:
#David-W-Fenton is correct. The better way to do this BeforeUpdate (not LostFocus)
Private Sub txtErrorComment_5_BeforeUpdate(Cancel As Integer)
If IsNull(Me.txtErrorComment_5) Then
MsgBox "Eh..! It cannot be Empty."
Cancel=true
End If
End Sub
To detect whether txtErrorComment_5 is blank, concatenate a Null string to its value and see whether the character length of the combined expression is zero.
Private Sub txtErrorComment_5_LostFocus()
If Len(Me.txtErrorComment_5 & vbNullString) = 0 Then
MsgBox "Eh..! It cannot be Empty."
End If
End Sub
If you want txtErrorComment_5 treated as blank when it contains only one or more space characters, include the Trim() function.
Private Sub txtErrorComment_5_LostFocus()
If Len(Trim(Me.txtErrorComment_5) & vbNullString) = 0 Then
MsgBox "Eh..! It cannot be Empty."
End If
End Sub