Link to home
Start Free TrialLog in
Avatar of angie_angie
angie_angie

asked on

How to write codes to get user input?

Hi Experts,

In my current procedures, I will pop up a form using DoCmd.OpenForm "FormName". This form constitutes several text boxes that I need the user to fill in the required information. Once the user finishes all the fields, he can click on a Enter botton for the information to be returned. How do I write the code to get the user input in this case? Additionally, can I just use coding to create such forms and later delete it after use? This is because I have to pop up different forms in different conditions. But I don't want to store all these pre-designed forms in my database.

Thank you very much!

P.S. I know how to use InputBox to pop up dialog box to get user input. But this only allows one piece of information at one time right? So I guess using form is a better choice.
Avatar of peter57r
peter57r
Flag of United Kingdom of Great Britain and Northern Ireland image

To address a control on a form you use:

Forms!formnamehere!controlnamehere

Although it is possible to develop forms on the fly, that is not the usual design approach for Access, and there are some situations where such a technique cannot be used.
You can make forms more flexible by buildng a general form and hiding unrequired controls in specific circumstances.
Yes! You can build a form at run time, run the created form, get user input, pass user input to your original code.
Interested,? I'll give it a try!

  I'd go with pre-defined dialog forms over creating ones on the fly.  Storage space is cheap.  At most, their only going to increase your database size by a few megabytes.  But in exchange, you get simpler debugging, get to write a *lot* less code, and save a ton of time by doing so.

JimD.
angie_angie,

This info is all over the web.

Simple example:
http://www.fontstuff.com/access/acctut08.htm

More complex example, with sample DB to download:
http://www.fontstuff.com/access/acctut19.htm

I have also attached my own sample

JeffCoachman
JeffCoachman
Access--Basic-Report-Query-Menu.mdb
Avatar of angie_angie
angie_angie

ASKER

boag2000,

I see your sample. Maybe I didn't make myself clear. I know how to write the values into tables. It is just that I don't know how to hide the form and then pop it up whenever neccessary. And how does the program know that it should stop executing and wait for the user to click on "Enter" button in order to get the values the user entered?
ASKER CERTIFIED SOLUTION
Avatar of Jeffrey Coachman
Jeffrey Coachman
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial


JDettman,
<a.  Code execution stops in the calling code until the form is closed or hidden.>
b.  The user can't switch away from it and must deal with the form.>
Just to clirify this point, I have this senario:
Created a from, two texboxes with labels, a Command Button with a _Click procedure.  The code creates the form and continues to execute till end of calling sub while the newly created form is waiting like any other form.

If it is a different understanding , can you post how to reproduce the effect mentioned?
Thanks
<<If it is a different understanding , can you post how to reproduce the effect mentioned?>>

  For the created form, set its modal and popup properties to True.  Then open it.

 JimD.
JDettman,
<For the created form, set its modal and popup properties to True.  Then open it.>

Did that as in this Sub. Form opened and,  MsgBox ("hi from original") executed while form is waiting for input.

Private Sub passingInput_Click()
    Dim frm As Form
    Set frm = CreateForm()    
    frm.Modal = True
    frm.PopUp = True
' ... other code to create controls and command button and event procedures
    DoCmd.OpenForm frm.Name
'... other code to initialize controls
   MsgBox ("hi from calling code")
End Sub

  odd...try:

  DoCmd.OpenForm frm.Name ,acNormal, , , acFormEdit, acDialog

 and let me know.

JimD.
JDettman,

    'DoCmd.OpenForm frm.Name
    DoCmd.OpenForm frm.Name, acNormal, , , acFormEdit, acDialog

No change. Original code continues to End Sub while the form is awaiting input!
Running Access 2007.
<<No change. Original code continues to End Sub while the form is awaiting input!
Running Access 2007.>>

  It's either something your doing or a change in A2007 and I'd really like to know if it's the latter.  I doubt it is however and this is a very common technique and if it had changed, it would have broken a lot of apps.

  I've been stuck on A2000 for a number of years now, but I did check this in 2003 and it works.  I created a form, called form 1 with one button on it with this:

   DoCmd.OpenForm "form2", acNormal, , , acFormEdit, acDialog
   MsgBox ("hi from calling code")

  Form2 is nothing but a form with a button on it to close the form.  When form1 runs, nothing happens of course until I click the button.  Form2 pops up and I don't get the "hi from calling code" msg until I click the close button on form2.

  However, I also tried simply setting the modal property alone and used this open:

   DoCmd.OpenForm "form2", acNormal, , , acFormEdit

   and it did not work.  Code in the calling form continued to execute.   Unless you specify acDialog as the window argument in the calling code, code execution continues regardless of the modal setting of the called form.

  I checked back into some old apps and I did that everywhere (specified acDialog in the open), so I didn't remember this 100% correctly, but it does do what you want as long as you specify acDialog unless A2007 has changed.  I don't have A2007 loaded at the moment anywhere so I can't check.  I have attached the test database though, so if you can try it in A2007 and let me know I'd appreciate it.

JimD.


db2.mdb
JDettman,
Interesting!
Run form1 and click button then compare opening an existing form with a code created form!
'Your form is an existing one, but what I refer to is a code created! One expects different behavior.

Private Sub Command0_Click()
 
    DoCmd.OpenForm "form2", acNormal, , , acFormEdit, acDialog
   
   MsgBox ("This message in a line after calling an existing form2")
   ' <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
   Dim frm As Form
    Dim ctlButton As Control
    Dim strFrm As String
    Dim x As Integer, y As Integer
    Dim dx As Integer, dy As Integer

    Dim mdl As Module
    Dim lngReturn As Long
   
    Set frm = CreateForm()
    strFrm = frm.Name
    frm.Modal = True
    frm.PopUp = True
    x = 1000 ' twips 1440/inch
    y = 100
    dx = 1440
    dy = 400

    DoCmd.Restore
   
     
    Set ctlButton = CreateControl(frm.Name, acCommandButton, , , , x, y + dy + dy, dx, dy)
    ctlButton.Caption = "check"
    ctlButton.Name = "cmdButton"
    Set mdl = frm.Module
    lngReturn = mdl.CreateEventProc("Click", ctlButton.Name)
    mdl.InsertLines lngReturn + 1, vbTab & "MsgBox ""Way cool!"""
    mdl.InsertLines lngReturn + 2, vbTab & "MsgBox ""Way cool second!"""
    mdl.InsertLines lngReturn + 3, vbTab & "MsgBox Me.name" & vbCrLf
    'mdl.AddFromFile "C:\c.txt" ' to add a cmdButton_Click event procedure
   
    'DoCmd.OpenForm frm.Name
    DoCmd.OpenForm frm.Name, acNormal, , , acFormEdit, acDialog

    MsgBox Me.Name
   
    'this line is executed without waiting for action with the newely created form
    MsgBox ("This message in a line after opening a !!!!recently created!!!! form")
   '>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
   
End Sub

db2.mdb
<<Run form1 and click button then compare opening an existing form with a code created form!
'Your form is an existing one, but what I refer to is a code created! One expects different behavior.>>

  A HA!   What the difference is is that your form is already open.  Your not creating it, saving, and closing it.  Add:

DoCmd.Close acForm,frm.Name,acSaveYes

  just before the open, which will save and close the form.  Now when the Open executes, the acDialog setting will take affect.

JimD.
That what I was saying, a saved form vs created form.
No need to save the form, because that was the request of the asker,
<can I just use coding to create such forms and later delete it after use? >
After finisfing the required input just close with nosave!