1

I have over 40 combo controls in my application. I am working on developing a public function, to put in the not in list event of every combo. The goal is to have 1 continuous pop up form, which will open, if the user says they want to add a new value to the combo. The open form command will pass open args for the

  1. record source
  2. the control source for the 1 text box on the continuous form (generically the type)
  3. the label for the control source.

I'm having some trouble getting it to pass the open args. I debug.print the parts of the args, I can split them, when they get passed (inconsistent results getting the open args to pass correctly, as I try to debug), and I cannot seem to set the record source for the pop up form correctly. I've tried doing 1 at a time, and still can't seem to get it.

This is the public function: Option Explicit

Public Function TypeNotInList(ctl As Control, arg1 As String, arg2 As Variant, arg3 As String)
    On Error GoTo Err_TypeNotInList
    Dim Msg, Style, Title
    'arg1 is the row source of the combo, to be passed as the recordsource for the frmAddTypeVal form
    'arg2 is the control source of the combo, to be passed as the control source for the text box in the frmAddTypeVal form
    'arg3 is the label of the combo, to be used for messages, and the label of the text box in the frmAddTypeVal form

    Msg = "The " & arg3 & " you entered is not in the " & arg3 & " list, would you like to add it now?"
    Style = vbYesNo
    Title = "Type or listing must be maintained"
    Response = MsgBox(Msg, Style, Title)

    If Response = vbYes Then

        ctl.Undo

        DoCmd.OpenForm "frmAddTypeVal", acNormal, , , , acDialog, arg1 & "|" & arg2 & "|" & arg3

        ctl.Requery

    End If

Exit_TypeNotInList:
        Exit Function

Err_TypeNotInList:
        MsgBox Err.Description
        Resume Exit_TypeNotInList
End Function

This is how I am calling it, in 1 combo's Not In List event: Option Explicit Private Sub FKAuditType_NotInList(NewData As String, Response As Integer) Dim a1 As String Dim a2 As String Dim a3 As String

    a1 = Me.FKTypeXYZ.RowSource
    a2 = "txtXYZType"
    a3 = Me.lblTypeXYZ.Caption

    TypeNotInList Me.FKTypeXYZ, a1, a2, a3

    Response = acDataErrContinue

End Sub

That should be calling the public function, and passing the 4 parameters.

This is the form load of the generic continuous pop-up form, called frmAddTypeVal: Option Explicit

Private Sub Form_Load()
    Dim VarArgs() As String

    VarArgs = Split(Me.OpenArgs, "|")

    Me.Form.RecordSource = VarArgs(0)
    Me.txtType.ControlSource = VarArgs(1)
    Me.lblType.Caption = VarArgs(2)

End Sub

When I run this as is, my debug.print (s) give me the following:

ctl = FKFKTypeXYZ
arg1 = SELECT tblXYZType.ID, tblXYZType.txtXYZType FROM tblXYZType ORDER BY tblXYZType.txtXYZType; 
arg2 = FKXYZType
arg3 = XYZ Type
openargs = 

I get each value, but the open args is null. What the heck, Beck?

Can anyone help guide this clueless coder? lol

Thanks!


I edited this to update the code, with the changes made. Now it's working! At least the first part of the process. The openargs get passed and the pop-up form works correctly. When I click close on that form, I'm back on the form with the notinlist combo. That resumes it's process and I get a message: The text you entered isn't an item in the list.

It know that's the default notinlist message. The thing is, the public function is supposed to handle this. It has, in the if Response = vbYes Then

ctl.undo
'undo trying to add a value that is not in the list yet

and then after the open form (which would involve the close of that form, I would think)

ctl.requery
'requery the combo, so the added value(s) can be seen

Anyone know how I can adjust this to prevent that message, but not just disable all warnings?

Thanks!


Got it! In the not in list, after I call the public function, I have to add:

Response = acDataErrContinue

This let's the default error message take a seat lol.

Thanks for all the help! This is going to make setting this up for every darn combo so much easier!!!!

missscripty
  • 529
  • 2
  • 11
  • 30
  • Try to replace `Dim VarArgs As Variant` with `Dim VarArgs() As Variant` to initialize it as an array. Does that help? – Erik A Aug 18 '17 at 13:14
  • I see `Dim oargs As Variant` at the top, where do you define the variable `openargs`? – dwirony Aug 18 '17 at 13:17
  • It may be that the form is open in edit mode maybe? Look at setting a form property instead. – Nathan_Sav Aug 18 '17 at 13:21
  • @ErikvonAsmuth that shouldn't make a difference, a `Variant` can store an array. If anything it should be `Dim args() As String` for an actually "strongly-typed" array. – Mathieu Guindon Aug 18 '17 at 13:32
  • Is `Option Explicit` specified at the top of the module? – Mathieu Guindon Aug 18 '17 at 13:33
  • Thank you, for all the replies! The oargs was an attempt to put the arg1 & "|" & arg2 & "|" & arg3 into 1 variable, so I could pass 1 openargs variable, but that isn't actually used in the code posted. I thought Option Explicit was at the top of the modules, for where the public function is, the form where the notinlist combo is, and the generic form. It wasn't, so I added it. I also changed the Dim VarArgs as String to Dim VarArgs() as String. – missscripty Aug 18 '17 at 14:12
  • Now the openargs gets passed and split correctly. My issue now, is that there are no errors, but when the form opens, the textbox control says #Name? No errors come up, but it is not setting the form's record source, and it's not setting the control source for the textbox control. The control source isn't surprising, because the record source never got set. After I try to set each aspect of the pop-up form, I added Debug.Print Me.Form.RecordSource Debug.Print Me.txtType.ControlSource Debug.Print me.lblType.Caption to see what they get set as, or not. – missscripty Aug 18 '17 at 14:12
  • Please edit the question to represent the current situation. Code in comments is hard to read with all the newlines missing. – Erik A Aug 18 '17 at 14:14
  • ok, thanks for the tip. Stack is so different from other forums. i will edit the original post. – missscripty Aug 18 '17 at 14:16

0 Answers0