Making a cell required based on combobox selection - ms-access

Nw to the forum and may as well be new to programming.
I would like to make 'Invoice No.' text box required, ONLY when 'INVOICED' is selected in my combobox.

If your form is bound, you could add the validation in the before update event like this:
Private Sub Form_BeforeUpdate(Cancel As Integer)
If Me.cboType = "INVOICED" And Nz(Me.InvoiceNo) = vbNullString Then
MsgBox "Invoice No. is required!", vbExclamation, Me.Caption
Me.InvoiceNo.SetFocus
Cancel = True
End If
End Sub
If unbound form, add the above statements (except Cancel = True) to your button click event.

Related

Check Box makes Label Visible

I have three Combo Boxes - When they are selected i would like it to check the tick box.
if the check box is ticked the Text box will then become Enabled, i currently have it set as disabled.
my question is how would i set the check box to be true when the three Combos are selected.
and how would i make it so that when the tick box is true it will enable the tex box
When all 3 combo boxes are set it will enable the checkbox. Once the value for any combo box is updated it calls a common function which checks whether all combo boxes have a value assigned and accordingly set the checkbox.
Private Sub cmbClientContact_AfterUpdate()
Call SetCheckBox
End Sub
Private Sub cmbClientName_AfterUpdate()
Call SetCheckBox
End Sub
Private Sub cmbProjectManager_AfterUpdate()
Call SetCheckBox
End Sub
Private Sub SetCheckBox()
If Nz(Me.cmbClientContact, "") <> "" And Nz(Me.cmbClientName, "") <> "" And Nz(Me.cmbProjectManager, "") <> "" Then
Me.Check25 = True
Me.Text27.Enabled = True
Else
Me.Check25 = False
End If
End Sub
Enable/disable textbox basis value of the checkbox
Private Sub Check25_AfterUpdate()
If Nz(Me.Check25, False) Then
Me.Text27.Enabled = True
Else
Me.Text27.Enabled = False
End If
End Sub
I would reccomend using the AfterUpdate event of all 3 combo boxes. Since the code is going to be the same (you're checking if all 3 combo boxes have a value), you can create one function to handle the check, and set that function to the AfterUpdate event of all 3 combo boxes when the form loads.
The function to update the controls (both the text box and check box) would be something like this:
Private Function UpdateControls()
Me.Text1.Enabled = Not (IsNull(Me.Combo1) Or IsNull(Me.Combo2) Or IsNull(Me.Combo3))
Me.Check1.Value = Not (IsNull(Me.Combo1) Or IsNull(Me.Combo2) Or IsNull(Me.Combo3))
End Function
You can call this function when the form initially loads, so the checkbox will be unchecked and the textbox will be disabled:
Private Sub Form_Load()
' update controls initially when the form loads
UpdateControls
End Sub
To make sure the same update happens whenever one of the combo box's values are updated, you can set each combobox's AfterUpdate event to the same function, like this:
Private Sub Form_Load()
' set each combo box's AfterUpdate event to run the check
Me.Combo1.AfterUpdate = "=UpdateControls()"
Me.Combo2.AfterUpdate = "=UpdateControls()"
Me.Combo3.AfterUpdate = "=UpdateControls()"
End Sub
So your final code might be something like this:
Private Sub Form_Load()
' set each combo box's AfterUpdate event to run the check
Me.Combo1.AfterUpdate = "=UpdateControls()"
Me.Combo2.AfterUpdate = "=UpdateControls()"
Me.Combo3.AfterUpdate = "=UpdateControls()"
' update controls initially when the form loads
UpdateControls
End Sub
Private Function UpdateControls()
Me.Text1.Enabled = Not (IsNull(Me.Combo1) Or IsNull(Me.Combo2) Or IsNull(Me.Combo3))
Me.Check1.Value = Not (IsNull(Me.Combo1) Or IsNull(Me.Combo2) Or IsNull(Me.Combo3))
End Function
Without knowing more specifics about the name schemas of your objects, this is my semi-vague answer:
One option (of many) is to use an On Click event procedure with the following:
If Not IsNull(Me.Combo1) _
And Not IsNull(Me.Combo2) _
And Not IsNull(Me.Combo3) Then
Me.Check1 = True
Me.Text1.Enabled = True
Else
Me.Check1 = False
Me.Text1.Enabled = False
End If
This assumes that the checkbox is named Check1 and the textbox is named Text1 and the comboboxes are Combo1, Combo2, and Combo3
It is a little confusing whether you meant Enabled or Visible, but if you meant Visible, just change the lines that say .Enabled to .Visible

MS Access Combobox not taking a value after requery shows value

I have Combobox that lets users either edit the related values for this field
- cboBEA using a button. I decided to use a NotInList routine whenever they want to ADD a new value. The edit part works without a hitch, but the NotInList part needs to accept a value after providing related fields in a popup form called frmBEA_JDIR
At first the requery of cboBEA wasn't working, so I did a more deliberate resetting of the rowsource by first setting it to "" then the actual SQL of the rowsource.
Here is the code in the "Save" button of the popup form frmBEA_JDR
Private Sub cmdSave_Click()
Select Case Me.OpenArgs
Case "Edit"
DoCmd.Save
DoCmd.Close acForm, "frmBEA_JDIR"
With Form_sfm_AddSPDistro
.cboBEA.Requery
.cboBESA.Requery
.cmbPROGRAM.RowSource = ""
.cmbPROGRAM.RowSource = "SELECT * FROM qLU_BEA_JDIR;"
.cmbPROGRAM = .cboBEA
End With
Case "AddNew"
Dim strSQL As String
strSQL = "SELECT LU_BEA_JDIR.ID, LU_BEA_JDIR.BEA, LU_BEA_JDIR.BESA, LU_BEA_JDIR.ORGANIZATION " _
& "FROM LU_BEA_JDIR;"
With Form_sfm_AddSPDistro
'cboBEA.Requery doesn't work, so...
.cboBEA.RowSource = ""
.cboBEA.RowSource = strSQL
.cboBESA.Requery
.cboBEA.Value = Me.txtBEA
.cmbPROGRAM.RowSource = ""
.cmbPROGRAM.RowSource = "SELECT * FROM qLU_BEA_JDIR;"
.cmbPROGRAM = .cboBEA
End With
DoCmd.Close acForm, "frmBEA_JDIR"
End Select
End Sub
Here is the NotInList event of the calling form:
Private Sub cboBEA_NotInList(NewData As String, Response As Integer)
Dim MsgBoxAnswer As Variant
Response = acDataErrContinue
Me!cboBEA.Undo 'Used this to prevent the requery error caused by frmBEA_JDIR
MsgBoxAnswer = MsgBox(NewData & " is not in the list. Do you want to add it?", vbQuestion + vbYesNo, "Add " & NewData & "?")
If MsgBoxAnswer = vbNo Then
Me.cboBEA = Null
DoCmd.GoToControl "cboBEA"
Else
DoCmd.OpenForm "frmBEA_JDIR", acNormal, , , acFormAdd, , "AddNew"
Form_frmBEA_JDIR.txtBEA = NewData
End If
End Sub
So depending on what calls this form - the NotInList or the Edit, I put it in the openargs parameter that calls frmBEA_JDIR. This is how I handle the update in the SAVE button. Again, the edit part works perfectly, but the AddNew from the NotInList event just won't populate cboBEA even after it is requeried and I can see the new value in it.
In brief: Instead of figuring out what to do in the popup with openargs, let acFormAdd do it for you; acFormAdd will open the form to a new record.
Send the new data in openargs.
Open frmBEA_JDIR in dialog mode. It stops the current code until the opened form is closed.
' open frmBEA_JDIR in dialog mode to add the new data.
DoCmd.OpenForm "frmBEA_JDIR", acNormal, , , acFormAdd, acDialog, "NewData"
'data is added, now requery the dropdown
cboBEA.Requery
Fool around with this without the 'case edit' section until you see it working. Then add the edit part using acFormEdit. You can check the datamode of the popped-up form to see if it's add or edit.

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

Dirty event not firing on Access 2007 form

I have a bound pop up form with a Clear data button. It runs the Undo command.
The Form_Current event sets this button's enable property to False.
The Form_Dirty event sets this button's enable property to True.
The button's property is always set to False even after I enter data into the form. I think this is because my Form_Load event is populating two fields. One is passed from the main form as an OpenArgs, the other is the unique ID which is calculated based on the OpenArgs value.
Any suggestions as to how to get the Dirty event to activate under these circumstances? If not, then an alternative approach perhaps?
Thanks in advance.
Code below:
Private Sub cmdUndoChanges_Click()
CustID_temp = Me!CustID
FacNo_temp = Me!Unique_ID
DoCmd.RunCommand acCmdUndo
Me!CustID = CustID_temp
Me!Unique_ID = FacNo_temp
End Sub
Private Sub Form_Current()
Me!cmdUndoChanges.Enabled = False
End Sub
Private Sub Form_Dirty(Cancel As Integer)
Me!cmdUndoChanges.Enabled = True
End Sub
Private Sub Form_Load()
Dim test As Integer
Me!CustID = OpenArgs
test = DCount("Unique_ID", "tbl_Table2", "CustID = '" & Me!CustID & "'")
If IsNull(Me!UNIQUE_No) Then
test = test + 1
Me!Unique_ID = CustID & "-" & test
MsgBox "No Previous Data"
Else
' these will be turned back on when the user selects the
' modify data button or add new data button.
For Each ctl In Me.Controls
Select Case ctl.ControlType
Case acTextBox, acComboBox, acOptionGroup, acCheckBox
ctl.Locked = True
ctl.BackColor = 15066597
Box40.BackColor = 15066597
End Select
Next ctl
End If
MsgBox Me!Unique_ID
End Sub
Have you tried Form_BeforeInsert() instead of Form_Dirty()?
I found another case where the Form_Dirty event never fires, the hard way. I thought I'd add it here since this is the top search result for the "not firing" question.
If your Form_Load function does anything to change the value of an existing record, the form will be set to dirty immediately, apparently before the logic that fires the event begins to run. Because of that there will never be an event if other data on the form is changed.
We had a "smart" Save button that was disabled until Form_Dirty fired. After I added a fixup to Form_Load for old records with a junk field, editing those records no longer ever enabled the Save button.

How to reference a textbox from another form?

I'm trying to modify existing code to add a popup box. This popup box is another form, and when a user clicks on a button on this form, I want a textbox on the base form to be populated. After that the popup disappears and then I need to access that value from the textbox on the base form.
The sequence should be:
Base form button click calls modal popup
Click in button on popup saves value to base form's textbox, then returns control.
Base form then uses this value to do something.
Base Form
Sub base()
DoCmd.OpenForm "PaperType", , , , , acDialog
MsgBox Me.TheAnswer 'This line gives a null error
End Sub
Popup Form
Private Sub btnRolls_Click()
'Me.Tag = 1
Forms!ReceiptDetail_sfrm!TheAnswer = 1
Me.Visible = False
End Sub
Private Sub btnSheets_Click()
'Me.Tag = 4
Forms!("ReceiptDetail_sfrm").TheAnswer = 4
Me.Visible = False
End Sub
You can do it that way, you probably need something like:
Private Sub btnRolls_Click()
Forms!ReceiptDetail_sfrm!TheAnswer = 1
Forms!ReceiptDetail_sfrm.Refresh
Me.Visible = False
End Sub
I've done what you're doing many times. I'm not at work to check my code right now for an example, but it's doable.
What I tend to do is hide the popup, then read from it:
Main Form:
Private Sub base()
DoCmd.OpenForm "PaperType", , , , , acDialog
'code waits for modal hide here
Me!TheAnswer = Forms("PaperType").SomethingOnThatFormThatStoresTheValue
DoCmd.Close acForm, "PaperType", acSaveNo
MsgBox Me.TheAnswer
End Sub
Popup
Private Sub btnRolls_Click
'may be hidden control
Me.SomethingOnThatFormThatStoresTheValue = TheValueToRead
Me.Visible = False
End Sub
Make sure that either A) Your popup can't be closed from the form itself, or B) Your calling code will handle the error of trying to read something that's no longer loaded (or both).
Apparently the correct answer is: "Don't do it this way!". Instead of trying to pass data between the forms, which is a pain, I made a table in Access which only has 1 field in 1 record. Then I store the value in there using DoCmd.RunSQL, and retrieve it from the other form using DLOOKUP().