How can I reset the checkbox to the default value? - ms-access

I have the following code, and if none of the If or ElseIf statements are satisfied, I'd like the checkbox to return to its original state. When you first view the checkboxes in an untouched state, they are filled in with a black box. Once touched/updated, they go to either a checked or empty state. Hoping this makes sense. Thanks!
Private Sub cgvhd_lim_AfterUpdate()
If cgvhd_lim Then
cgvhd_limext = "Limited"
ElseIf cgvhd_ext1 Then
cgvhd_limext = "Extensive"
ElseIf cgvhd_ext2 Then
cgvhd_limext = "Extensive"
ElseIf cgvhd_ext3 Then
cgvhd_limext = "Extensive"
ElseIf cgvhd_ext4 Then
cgvhd_limext = "Extensive"
ElseIf cgvhd_ext5 Then
cgvhd_limext = "Extensive"
Else
cgvhd_limext = 0
End If
End Sub

To return a checkbox to its default state:
cgvhd_lim = Null

Related

Trying to connect 6 different tables to a lookup field, based on results entered into another lookup field

The code below was my attempt to make it work with VBA, but don't know how to link the code in, or it doesn't work?
Option Compare Database
If siteType = "Pennies" Then
siteModel = TblPennies
ElseIf siteType = "Nickels" Then
siteModel = TblNickels
ElseIf siteType = "Dimes" Then
siteModel = TblDimes
ElseIf siteType = "Quarters" Then
siteModel = TblQuarters
ElseIf siteType = "Half Dollars" Then
siteModel = TblHalfDollars
ElseIf siteType = "Dollars" Then
siteModel = TblDollars
End If
End Function
I expect form field Model to display only the choices for the Type field entered. I only get blanks now. Thank you
Assume siteType and siteModel are names of comboboxes. Simplify code.
Me.siteModel.RowSource = "Tbl" & Replace(Me.siteType, " ", "")
Me.siteModel.Requery
Now figure out what event(s) to put code into. Suggest form Current and siteType AfterUpdate.

How do I set image visible if case is met?

I have a report I'm trying to create. One of the options in the form is Symbol with a dropbox for users to select X, /, or -
If the X is visible, I want the respective image (large red version of symbol) to be visible.
I have built event on Report Load:
Private Sub Report_Load()
Select Case Me.Symbol
Case "X"
Me.ImageX.Visible = True
Me.ImageDash.Visible = False
Me.ImageDiagonal.Visible = False
Case "-"
Me.ImageX.Visible = False
Me.ImageDash.Visible = True
Me.ImageDiagonal.Visible = False
Case "/"
Me.ImageX.Visible = False
Me.ImageDash.Visible = False
Me.ImageDiagonal.Visible = True
Case Else
Me.ImageX.Visible = True
Me.ImageDash.Visible = True
Me.ImageDiagonal.Visible = True
End Select
End Sub
Any ideas on what I'm missing?
As suggested by June, use the On Format event for the relevant section in which the objects reside, as the various object references do not hold values until after the On Load event of the report. For more information regarding the order of events, you may wish to refer to this documentation.
I might also suggest condensing your code to the following:
Private Sub Detail_Format()
Me.ImageX.Visible = Me.Symbol = "X"
Me.ImageDash.Visible = Me.Symbol = "-"
Me.ImageDiagonal.Visible = Me.Symbol = "/"
End Sub

Runtime Error 438 Hiding Page

I receive a
run-time error 438
when changing visibility of a page in Access VBA. The code below is called "AfterUpdate" for a listbox. I believe my syntax is correct after much review. I've tested the conditional statements with MsgBox's successfully.
Any ideas?
Public Sub RefreshControlTabs()
If [Forms]![frmPrintPostage_Email_DB]![subfrm_CampaignData]![Channel].Value = "Mail" Then
[Forms]![frmPrintPostage_Email_DB]![subfrm_CampaignData].[tabChannelControl].Pages(0).Visible = True
[Forms]![frmPrintPostage_Email_DB]![subfrm_CampaignData].[tabChannelControl].Pages(1).Visible = False
ElseIf [Forms]![frmPrintPostage_Email_DB]![subfrm_CampaignData]![Channel].Value.Value = "Email" Then
[Forms]![frmPrintPostage_Email_DB]![subfrm_CampaignData].[tabChannelControl].Pages(1).Visible = True
[Forms]![frmPrintPostage_Email_DB]![subfrm_CampaignData].[tabChannelControl].Pages(0).Visible = False
Else
'do nothing
MsgBox "Valid Email/Mail channel not found for this job."
[Forms]![frmPrintPostage_Email_DB]![subfrm_CampaignData].[tabChannelControl].Pages(0).Visible = True
[Forms]![frmPrintPostage_Email_DB]![subfrm_CampaignData].[tabChannelControl].Pages(1).Visible = False
End If
End Sub
Perhaps your line:
ElseIf [Forms]! ... [Channel].Value.Value = "Email" Then
Should be:
ElseIf [Forms]! ... [Channel].Value = "Email" Then

Using Passed Variable to Select Items like "Me.myvar.Item"

I am trying to create a new function that I can pass variables to and those variables will be used to select and modify items.
Function
Function TabDisplay(Switch As String, TargetTab As String)
If (Me. & Switch & .Value) = -1 Then
Me.Tabs.Pages.Item("TargetTab").Visible = True
ElseIf Me.Switch.Value = 0 Then
Me.Tabs.Pages.Item(TargetTab).Visible = False
Else
Me.Switch.Value = "0"
End If
End Function
How I call the function
TabDisplay "SoftwareInstallable", "tabSoftware"
Working Code that I used before attempting this function
If Me.SoftwareInstallable.Value = -1 Then
Me.Tabs.Pages.Item("tabSoftware").Visible = True
ElseIf Me.SoftwareInstallable.Value = 0 Then
Me.Tabs.Pages.Item("tabSoftware").Visible = False
Else
Me.SoftwareInstallable.Value = "0"
End If
====Final Code====
Function
Function TabDisplay(Switch As String, TargetTab As String)
If Me.Controls(Switch).Value = -1 Then
Me.Tabs.Pages.Item(TargetTab).Visible = True
ElseIf Me.Controls(Switch).Value = 0 Then
Me.Tabs.Pages.Item(TargetTab).Visible = False
Else
Me.Controls(Switch).Value = "0"
End If
End Function
Function Call
TabDisplay "SoftwareInstallable", "tabSoftware"
I think I understand what you want. But let me describe it with a different example.
I have a form with a text box named txtFoo. So I can Debug.Print its value to the Immediate window like this ...
Debug.Print Me!txtFoo.Value
Later I decide I want to store the control name in a variable so that I can Debug.Print the value of any other control simply by changing the variable.
Dim strControlName As String
strControlName = "txtBar"
Then I can use that variable to reference the matching item in the form's Controls collection, and obtain its value.
Debug.Print Me.Controls(strControlName).Value
I think you can use that approach to accomplish what you want, but I won't attempt to rewrite your code sample because I suspect I would get it wrong.

VBA code that changes the background color of a form after update

I need some code that when a check box is unchecked it will change the background color of my form and return it back to its original color when checked. The code i have for the check box currently locks a combo box when a value is chosen. Example below
Private Sub AccessKeyNo_AfterUpdate()
If MsgBox("Do you want to assign Access Key " & Me.AccessKeyNo & "?", _
vbYesNo) = vbYes Then
Me.GuestAccessKeyID = Me.AccessKeyNo
If Me.Dirty Then Me.Dirty = False
Me.AccessKeyNo.Requery
Me.AccessKeyNo = Null
Me.MyCheckBox = IsNull(Me.GuestAccessKeyID)
End If
End Sub
In a standard module (not the form module -- the scope of the constants would be limited to form, thus you wouldn't be able to reuse them):
Public Const colorBlue_Cornflower = "15714765"
Public Const colorTan_Encarnacion = "11398133"
Now in the module for the form:
Dim colorThis as String, booWhatever as Boolean
booWhatever = Me.MyCheckBox ''Use of the variable can prevent problems
If booWhatever Then
colorThis = colorBlue_Cornflower
Else
colorThis = colorTan_Encarnacion
End If
subFrm.Form.Section(acDetail).BackColor = colorThis
subFrm.Form.Section(acHeader).BackColor = colorThis
subFrm.Form.Repaint