Solved

How to write codes to get user input?

Posted on 2008-06-24
16
2,226 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 30

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 57
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
 
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 250 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 57

Assisted Solution

by:Jim Dettman (Microsoft MVP/ EE MVE)
Jim Dettman (Microsoft MVP/ EE MVE) earned 250 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 30

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
Free Gift Card with Acronis Backup Purchase!

Backup any data in any location: local and remote systems, physical and virtual servers, private and public clouds, Macs and PCs, tablets and mobile devices, & more! For limited time only, buy any Acronis backup products and get a FREE Amazon/Best Buy gift card worth up to $200!

 
LVL 57
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 30

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 57
ID: 21869199

  odd...try:

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

 and let me know.

JimD.
0
 
LVL 30

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 57
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 30

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 57
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 30

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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Most if not all databases provide tools to filter data; even simple mail-merge programs might offer basic filtering capabilities. This is so important that, although Access has many built-in features to help the user in this task, developers often n…
Overview: This article:       (a) explains one principle method to cross-reference invoice items in Quickbooks®       (b) explores the reasons one might need to cross-reference invoice items       (c) provides a sample process for creating a M…
What’s inside an Access Desktop Database. Will look at the basic interface, Navigation Pane (Database Container), Tables, Queries, Forms, Report, Macro’s, and VBA code.
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…

758 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now