How do I set image visible if case is met? - ms-access

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

Related

Three Options - Enabling/Disabling based on input and record value

I have a form where (among other things) there are three options which are intended to be exclusive.
optDealerPay
optCustomerPay
optNoTradeIn
I want only one to be selected at a time, but when I originally designed this, I made these into three separate table columns instead of one and using an option group. Mostly because of the output I'm wanting on a report I'm working on, too. Anyway, when one is selected, the other two go Enabled = False. The issue I'm having is when the form loads (actually using Form_Current() to ensure the record is loaded prior to "checking"), the form will disable one of the two options that are unselected, but not the other one. Here is a picture:
And here is the code I'm using:
Private Sub Form_Current()
If Me.optDealerPay Then
Me.optCustomerPay.Enabled = False
Me.optNoTradeIn.Enabled = False
Else
Me.optCustomerPay.Enabled = True
Me.optNoTradeIn.Enabled = True
End If
If Me.optCustomerPay Then
Me.optDealerPay.Enabled = False
Me.optNoTradeIn.Enabled = False
Else
Me.optDealerPay.Enabled = True
Me.optNoTradeIn.Enabled = True
End If
If Me.optNoTradeIn Then
Me.optDealerPay.Enabled = False
Me.optCustomerPay.Enabled = False
Else
Me.optDealerPay.Enabled = True
Me.optCustomerPay.Enabled = True
End If
End Sub
What's a better method to accomplish this outside of redesigning the table structure, which I'm really trying to avoid if possible. Similar code is used with the AfterUpdate() procedure on each of the three options.
Your code will execute every if statement one by one - that is some kind of mess. Make all radiobuttons enabled in design mode and try this code:
Private Sub Form_Current()
If Me.optDealerPay Then
Me.optCustomerPay.Enabled = False
Me.optNoTradeIn.Enabled = False
ElseIf Me.optCustomerPay Then
Me.optDealerPay.Enabled = False
Me.optNoTradeIn.Enabled = False
ElseIf Me.optNoTradeIn Then
Me.optDealerPay.Enabled = False
Me.optCustomerPay.Enabled = False
Else
' Maybe you should do something if none of the options is true
End If
End Sub
Your current code could essentially be reduced to:
Private Sub Form_Current()
optDealerPay.Enabled = Nz(optDealerPay, 0)
optCustomerPay.Enabled = Nz(optCustomerPay, 0)
optNoTradeIn.Enabled = Nz(optNoTradeIn, 0)
End Sub
That is, if a radio button is null or unchecked, then it is disabled; else it is enabled.
However, although this is the behaviour that you have requested, I'm not sure that this is desirable, as once the user has select an option they cannot change it.

Viewing combobox values from textbox values Microsoft Access 2007

I have a form ("Patient Complications") where users input data to a form using 2 cascading combo boxes ("catcombo" and "speccombo"). The combo boxes pull their values from a table ("Complications"). The table has a field for complication category (for example, infection, bleeding, mechanical). A second field lists specific complications (for example, if the complication category is "bleeding", the specific complication could be "GI" or "other"). The input from the combo boxes are concatenated and put into a text field on the form ("Complication"). That part works fine.
My form has several command buttons, including "edit" and "save" command buttons. Since I don't want users interacting with the "complication" field on the form, I have the field become not visible when the "edit" button is clicked. Instead, the 2 combo boxes become visible and allow users to input data. When "save" is selected, the reverse occurs. The two combo boxes become not visible, and the complication field becomes visible and locked.
Unfortunately, when "edit" is selected, the combo boxes are visible but show up blank (nothing is selected or displayed). I am trying to have the boxes display the inputs that were given to the text field. For example, if the text field displays "Bleeding, Other", I want the catcombo box to display "Bleeding" and the speccombo box display "Other". I have been unable to find anything to this effect. If anyone has any ideas it would be greatly appreciated.
The relevant code is included below. Please let me know if I can provide further clarification.
Private Sub catcombo_AfterUpdate()
Me.speccombo.Requery
End Sub
Private Sub speccombo_OnCurrent()
Dim strsql As String
strsql = "SELECT [Complications]![Specific Complication] FROM tblComplications" & _
"WHERE [Complication Category]=Forms![Patient Complications]![catcombo].value"
End Sub
Private Sub speccombo_AfterUpdate()
Forms![Patient Complications]![Complication] = Me.catcombo.Value & ", " & Me.speccombo.Value
End Sub
Private Sub save_Click()
Me.recordcount.Caption = "Record " & Me.CurrentRecord & " of " & Me.Recordset.recordcount
Me.Patient_Initials.Visible = False
Date_of_Complication.Locked = True
Complication.Visible = True
Complication.Locked = True
comments.Locked = True
catcombo.Visible = False
speccombo.Visible = False
Me.edit.Visible = True
Me.edit.SetFocus
Me.help.Visible = False
Me.save.Visible = False
Me.first.Visible = True
Me.next.Visible = True
Me.previous.Visible = True
Me.last.Visible = True
Me.addnew.Visible = True
Me.close.Visible = True
Me.cancel.Visible = False
End Sub
Private Sub edit_Click()
Me.recordcount.Caption = "Record " & Me.CurrentRecord & " of " & Me.Recordset.recordcount
Me.Patient_Initials.Visible = False
Date_of_Complication.Locked = False
Complication.Visible = False
comments.Locked = False
catcombo.Visible = True
catcombo.Locked = False
catcombo.Enabled = True
speccombo.Visible = True
speccombo.Locked = False
speccombo.Enabled = True
Me.cancel.Visible = True
Me.cancel.SetFocus
Me.edit.Visible = False
Me.help.Visible = True
Me.save.Visible = True
Me.first.Visible = False
Me.next.Visible = False
Me.previous.Visible = False
Me.last.Visible = False
Me.addnew.Visible = False
Me.close.Visible = False
End Sub
I figured it out. I added a field to the "Complications" table called "Input". This field contains the concatenated values that were put into the patient record (in the example above, the input field would be "Bleeding, Other"). The values in the Input field are the exact values that would be recorded in the "Complication" field on the Patient Complications form. I added the vba code below to the "Edit" command button code.
If Not IsNull(Forms![Patient Complications]![Complication]) Then
Dim comptext As String
Dim spectext As String
comptext = DLookup("[Complication Category]", "Complications", "Input = Forms![Patient Complications]![Complication]")
catcombo.Value = comptext
spectext = DLookup("[Specific Complication]", "Complications", "Input=Forms![Patient Complications]![Complication]")
speccombo.Value = spectext
End If
That solved my initial problem, but I then had an issue with the speccombo box displaying the last value it was given. For example, if speccombo.value="GI" when I click "edit", it would continue to display "GI" until another selection was made. This isn't a huge deal, just inconvenient. I wanted to make the speccombo box essentially zero out if catcombo was changed. I added the code below to fix that problem.
Private Sub catcombo_AfterUpdate()
Me.speccombo.Requery
Me.speccombo.Value = Null
End Sub
Please let me know if I need to provide clarification for anything.

"Add Record" gives "Can't go to specified record" after first use Access

I have a command button in a form that allows users to add a new record. The command only works the first time you click it after the form is opened. I can navigate through the records without any errors and the first time you add a record after the form is opened will not give errors. Once you try to add a second record, the "Can't go to the specified record" error message is displayed.
I can make edits in the query without a problem and I can edit things without a problem. If I close out of the form and reopen it I can add a new record without issues. Anyone have ideas?
Using VBA, it's the standard add-new code, nothing else. Posted all the code for this form below. Some of the command buttons listed are in macros, don't know if that makes a difference.
Private Sub add_Click()
DoCmd.GoToRecord , , acNewRec
End Sub
Private Sub edit_info_Click()
Date_of_Echo.Locked = False
ID.Locked = False
AoV.Locked = False
AI.Locked = False
MR.Locked = False
TR.Locked = False
TR_velocity.Locked = False
PA_pressures.Locked = False
LVeDD.Locked = False
LVeSD.Locked = False
RV_function.Locked = False
comments.Locked = False
Me.next.Visible = False
Me.previous.Visible = False
Me.first.Visible = False
Me.last.Visible = False
Me.add.Visible = False
Me.save.Visible = True
Me.save.SetFocus
Me.edit_info.Visible = False
End Sub
Private Sub Form_Current()
If Me.NewRecord Then
Me.recordcounter.Caption = "New Record"
Me.next.Visible = False
Me.previous.Visible = False
Me.first.Visible = False
Me.last.Visible = False
Me.add.Visible = False
Me.edit_info.Visible = False
Me.save.Visible = True
Date_of_Echo.Locked = False
ID.Locked = False
AoV.Locked = False
AI.Locked = False
MR.Locked = False
TR.Locked = False
TR_velocity.Locked = False
PA_pressures.Locked = False
LVeDD.Locked = False
LVeSD.Locked = False
RV_function.Locked = False
comments.Locked = False
Else
Me.recordcounter.Caption = "Record " & Me.CurrentRecord & " of " & Me.Recordset.RecordCount
Me.next.Visible = True
Me.previous.Visible = True
Me.first.Visible = True
Me.last.Visible = True
Me.add.Visible = True
Me.edit_info.Visible = True
Me.save.Visible = False
End If
End Sub
Private Sub save_Click()
DoCmd.save
Me.previous.Visible = True
Me.first.Visible = True
Me.last.Visible = True
Me.next.Visible = True
Me.add.Visible = True
Me.edit_info.Visible = True
Me.recordcounter.Caption = "Record " & Me.CurrentRecord & " of " & Me.Recordset.RecordCount
Me.add.SetFocus
Me.save.Visible = False
Date_of_Echo.Locked = True
ID.Locked = True
AoV.Locked = True
AI.Locked = True
MR.Locked = True
TR.Locked = True
TR_velocity.Locked = True
PA_pressures.Locked = True
LVeDD.Locked = True
LVeSD.Locked = True
RV_function.Locked = True
comments.Locked = True
End Sub
Private Sub next_Click()
On Error GoTo Err_cmdLastRecord_Click
Me.AllowAdditions = False
DoCmd.GoToRecord , , acNext
Exit_cmdLastRecord_Click:
Exit Sub
Err_cmdLastRecord_Click:
MsgBox " There are no more records ", vbExclamation, ""
Resume Exit_cmdLastRecord_Click
End Sub
My first thought is that your record isn't saving before you try to go to a new record so try this and see if it fixes the issue.
Private Sub add_Click()
If Me.Dirty Then
Me.Dirty = False
End If
DoCmd.GoToRecord , , acNewRec
End Sub
If anyone is still having this problem, I can't explain why it's happening, but here's a work around:
Reset the AllowAdditionsProperty on the form:
Me.[form_name].Form.AllowAdditions = False
Me.[form_name].Form.AllowAdditions = True
This was a very strange issue because my code had been working fine for weeks. I was using something like this:
Me.frm.SetFocus
DoCmd.GoToRecord , , acNewRec
!Field1 = value1
!Field2 = value2
It was working great, and then out of the blue I started getting the "Can't go to Specified Record" error, even though I had made no changes to the code. The form settings were fine and I could add records all day long in the underlying table. I even checked the form's AllowAdditions property in the Immediate window, and that showed it was set to True. But I tried this work around, and it worked.
Now I add those two lines after every instance of Docmd.GoToNewRoecord, acNewRecord and it's working.
Another thing I have mentioned with the same problem is that you should check the Form data source query. Are all the FIELDS available, are the JOINS correct?
In my case I changed indexed joins from text fields to numeric and it needed update also in Form Data source query.

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 - "Compile Error: Method or data member not found"

I have been developing an Access form to operate as a frontend for a SQL database. I have been working with a developer, and they added the following VBA code to our main form:
Private Sub Form_Current()
If Me.NewRecord = True Then
Me.Client_Name.Enabled = True
Me.SSN.Enabled = True
Me.DOB.Enabled = True
Me.Prob_Fee.Enabled = True
Me.Settle_Atty_Amt.Enabled = True
Me.Settle_Date.Enabled = True
Me.Final_Date.Enabled = True
Else
Me.Client_Name.Locked = True
Me.SSN.Locked = True
Me.DOB.Locked = True
Me.Prob_Fee.Locked = True
Me.Settle_Atty_Amt.Locked = True
Me.Settle_Date.Locked = True
Me.Final_Date.Locked = True
End If
End Sub
When I try to add a new variable to this statement Me.Case_ID.Locked = True, the following error is returned:
Compile Error: Method or data member not found
I'm not sure where to go from here.
Make sure that you are referring to the name of the control that you just added. It may not be the same as the field / column contained. On the 'Other' tab of the property sheet, you will find Name, that is the property you need. It is often different from the name of the control contained.