Get form field value via module [VBA]

前端 未结 3 1452
走了就别回头了
走了就别回头了 2021-01-27 06:25

I have a program built with VBA, in access. I have a form with the field chDate and I need to get the value of the field in a module file named Global (not class module).

<
相关标签:
3条回答
  • 2021-01-27 06:43

    If you can provide some sample code, it might help us in trying to get you a solution.

    Here's what might work:

    Dim an object as an instance of your form, and set the instance to a new instance. These two lines will do that (assuming the form is called frmForm)

    Dim theForm as frmForm
    Set theForm = new frmForm
    

    then Show that Form:

    theForm.show
    

    The form will get the focus, so you can populate the fields. At that point, once the form is hidden, your code should be able to pull the field as such:

    var1 = theForm.txtFormField.Text
    

    However, if you unload the form in code, all variables directly tied to the form will be lost. In that case, you might want to follow Oneide's example, and set a global variable to the value of the form field. You can do this in one of the form's event handlers.

    0 讨论(0)
  • 2021-01-27 06:43

    As far as I know, you're trying to get the data the wrong way.

    Anytime you would try to get your form data, from a standard module, your form probably will already be closed or, if you're not opening it as a restricted window, the value will not be ready yet.

    Anyway, if you could get pass through all the difficulties above, you're probably coding in a not standard / right way.

    Remember that VBA is an event driven language, and you would be better using it's strenghts rather than fighting against them.

    As a final word, if this is just a casual coding, I suggest you using a global variable and make de Code Behind Form have set it up on control's change event.

    If you're not sure what I mean, please leave a comment and I'll try to explain it better.

    And, if you don't mind, let us know more about your starting code to get better answers.

    0 讨论(0)
  • 2021-01-27 06:52

    Let me see if I can recall my Access days. In your global module you should access the form field be prefixing the form name i.e. var1 = Form1.txtFirmField.Text

    0 讨论(0)
提交回复
热议问题