Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

How to write codes to get user input?

Posted on 2008-06-24
16
Medium Priority
?
2,305 Views
Last Modified: 2013-11-28
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.
0
Comment
Question by:angie_angie
  • 6
  • 6
  • 2
  • +2
16 Comments
 
LVL 77

Expert Comment

by:peter57r
ID: 21853741
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.
0
 
LVL 31

Expert Comment

by:hnasr
ID: 21854410
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!
0
 
LVL 58
ID: 21854530

  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.
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
LVL 74

Expert Comment

by:Jeffrey Coachman
ID: 21861292
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
0
 

Author Comment

by:angie_angie
ID: 21862271
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?
0
 
LVL 74

Accepted Solution

by:
Jeffrey Coachman earned 750 total points
ID: 21862447
angie_angie,


<I don't know how to hide the form and then pop it up whenever neccessary.>
...To hide a form:
Forms!YourForm.Visible = False

...To un-hide a form:
Forms!YourForm.Visible = True

JeffCoachman
0
 
LVL 58

Assisted Solution

by:Jim Dettman (Microsoft MVP/ EE MVE)
Jim Dettman (Microsoft MVP/ EE MVE) earned 750 total points
ID: 21863688
<<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?>>

 Typically, one makes such "dialog" forms popup and modal.  Popup places it on top of all other windows, modal ensures that:

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.

JimD.
0
 
LVL 31

Expert Comment

by:hnasr
ID: 21868299


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
0
 
LVL 58
ID: 21868353
<<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.
0
 
LVL 31

Expert Comment

by:hnasr
ID: 21869090
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
0
 
LVL 58
ID: 21869199

  odd...try:

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

 and let me know.

JimD.
0
 
LVL 31

Expert Comment

by:hnasr
ID: 21869620
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.
0
 
LVL 58
ID: 21874499
<<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
0
 
LVL 31

Expert Comment

by:hnasr
ID: 21875435
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
0
 
LVL 58
ID: 21876944
<<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.
0
 
LVL 31

Expert Comment

by:hnasr
ID: 21877692
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!
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The Windows Phone Theme Colours is a tight, powerful, and well balanced palette. This tiny Access application makes it a snap to select and pick a value. And it doubles as an intro to implementing WithEvents, one of Access' hidden gems.
Microsoft Access is a place to store data within tables and represent this stored data using multiple database objects such as in form of macros, forms, reports, etc. After a MS Access database is created there is need to improve the performance and…
In Microsoft Access, learn how to “cascade” or have the displayed data of one combo control depend upon what’s entered in another. Base the dependent combo on a query for its row source: Add a reference to the first combo on the form as criteria i…
This lesson discusses how to use a Mainform + Subforms in Microsoft Access to find and enter data for payments on orders. The sample data comes from a custom shop that builds and sells movable storage structures that are delivered to your property. …

926 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question