p2p.wrox.com Forums

p2p.wrox.com Forums (http://p2p.wrox.com/index.php)
-   Access (http://p2p.wrox.com/forumdisplay.php?f=18)
-   -   Where do i put the code? (http://p2p.wrox.com/showthread.php?t=9525)

bph February 18th, 2004 05:34 PM

Where do i put the code?
 
I have some code I would like to see run after a form has opened and is viewable by the user.

It's an input box which asks a user to enter his or her initials. On open I set focus to the textbox that I would like the entry made. Then I would like the input box to come up allowing the entry.

I tried the lost focus event, but if the user goes back to the textbox, it comes at them again and that isn't good.

Here's the code I'm using. Anyway around the user taking cancel and totally avoiding this entry? It can't be null, and the user can't do a cancel. Aside from required set on textbox which I curently use.
*****************************************
Dim strInput As String, strMsg As String
strMsg = "Please enter your Initials."
strInput = InputBox(Prompt:=strMsg, _
Title:="Action Required", XPos:=5000, YPos:=5000)
MsgBox "Thank You, " & strInput

********Thanks, BPH

SerranoG February 18th, 2004 05:54 PM

Put this on the form's ON OPEN event, assuming that the textbox that holds the initials is called Me.txtInitials:
Code:

  Dim strInitials as String

   strInitials = ""
   Do While strInitials = ""
      strInitials = Trim(InputBox("Please enter your initials.","User Initials"))
   Loop
   Me.txtInitials = strInitials
   Me.txtInitials.SetFocus

   The do loop is there to prevent someone from entering nothing as initials. It will keep asking until the person enters something. If you don't specify a box location, the input box will default to dead center of the screen.



Greg Serrano
Michigan Dept. of Environmental Quality, Air Quality Division

bph February 18th, 2004 11:06 PM

Thanks Greg. I'll give it a shot.

[size=1]
Quote:

quote:Originally posted by SerranoG
 Put this on the form's ON OPEN event, assuming that the textbox that holds the initials is called Me.txtInitials:
Code:

  Dim strInitials as String

   strInitials = ""
   Do While strInitials = ""
      strInitials = Trim(InputBox("Please enter your initials.","User Initials"))
   Loop
   Me.txtInitials = strInitials
   Me.txtInitials.SetFocus

   The do loop is there to prevent someone from entering nothing as initials. It will keep asking until the person enters something. If you don't specify a box location, the input box will default to dead center of the screen.



Greg Serrano
Michigan Dept. of Environmental Quality, Air Quality Division

bph February 19th, 2004 09:31 AM

This works well. But..There's always a what if....the user decides she/he doesn't want to go into the form after all. How can allow for a graceful exit from the input box?


SerranoG February 19th, 2004 09:43 AM

Quote:

quote:Originally posted by bph
 This works well. But..There's always a what if....the user decides she/he doesn't want to go into the form after all. How can allow for a graceful exit from the input box?
 In that case, skip the DO LOOP altogether and just accept any initials. Check for no input.
Code:

  Dim varInitials as Variant

   varInitials = InputBox("Please enter your initials.","User Initials")
   If IsNull(varInitials) Then
      'No initials entered.  Assume they don't want to put any.
      'Enter some code here to do something else, e.g. close the form.
   Else
      Me.txtInitials = Trim(varInitials)
      Me.txtInitials.SetFocus
   End If






Greg Serrano
Michigan Dept. of Environmental Quality, Air Quality Division

bph February 19th, 2004 12:12 PM

Excellent...
Here's what I ended up with. I incorporated your thoughts into the final product. If the user doesn't enter initials they go nowhere. If they enter initials that don't match the initials in a table that sits behind the initial field on the form, then I let the form open and they can't proceed until they select their initials from a drop down box.
Now....FYI- this is a data entry form. I want this same input box routine to run after a user has saved their entries, and moved to input data in the next record. So They save, next blank form appears and boom, the have to enter their initials.

I tried the "after insert" event, and the "after update" event. The input box pops up too early, before the next blank record is viewed. Want to see the next blank record, then prompt for initials.

Make sense? Where do I stick this code to make that happen?

****************************
Private Sub Form_Open(Cancel As Integer)
On Error GoTo NoInitialsEntered
Dim varInitials As Variant
   varInitials = InputBox("Please enter your initials.", "User Initials")
   If varInitials = "" Then
   DoCmd.Close acForm, "add_frm_lake_pipeinfo", acSaveNo
   Else
   Me.cboLake_Person_Last_Updated.SetFocus
   Me.cboLake_Person_Last_Updated.Text = (varInitials)
   End If
NoInitialsEntered:
    Select Case Err.Number
        Case 2237
            MsgBox "The initials entered are not valid"
            Exit Sub
        End Select
End Sub


SerranoG February 19th, 2004 12:19 PM

Move the entire code from the ON OPEN event to the ON CURRENT event. This will activate it everytime you move from record to record. However, if you DON'T want it happen for previously entered records and you ONLY want it to happen to NEW records, you need to surround the whole thing with
Code:

  If Me.NewRecord
      ...
   End If



Greg Serrano
Michigan Dept. of Environmental Quality, Air Quality Division

bph February 19th, 2004 02:50 PM

Got it.! Thanks, BPH


All times are GMT -4. The time now is 04:22 AM.

Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.