Wrox Programmer Forums
|
Access VBA Discuss using VBA for Access programming.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Access VBA section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old April 7th, 2009, 04:50 PM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 126
Thanks: 5
Thanked 0 Times in 0 Posts
Default InputBox with Loop issue

Hello!
I have a slight issue...I have the code below, but if the user clicks on the cancel button, it will not cancel out of the loop...I can't remember how to fix this...any ideas?

Code:
 
    Dim ReportSize As String
 
    DoCmd.RunCommand acCmdSaveRecord
    ReportSize = InputBox("Please enter the number cooresponding to the paper size " _
    & "you wish to print on." & _
    vbCrLf & "1 - Letter" & _
    vbCrLf & "2 - A4", "Please choose your paper size")
 
    Do While ReportSize <> "1" And ReportSize <> "2"
 
        ReportSize = InputBox("Please enter the number cooresponding to the paper size " _
        & "you wish to print on." & _
        vbCrLf & "1 - Letter" & _
        vbCrLf & "2 - A4", "Please choose your paper size")
 
    Loop
 
    If ReportSize = "1" Then
 
        OpenReport_FX "rptECNForm", acViewNormal, "", _
        "[CCNNumber]=[Forms]![frm2009_11]![CCNNumber]"
 
    End If
 
    If ReportSize = "2" Then
 
        OpenReport_FX "rptECNForm_A4", acViewNormal, "", _
        "[CCNNumber]=[Forms]![frm2009_11]![CCNNumber]"
 
    End If
 
    Exit Sub
Thanks in advance for any help.

Laura
 
Old April 8th, 2009, 08:00 AM
Friend of Wrox
 
Join Date: Mar 2004
Posts: 3,069
Thanks: 0
Thanked 10 Times in 10 Posts
Default

I am not sure why you want to force the user to input a string when they click cancel by keeping them in a loop. But you could do this:

Code:
If ReportSize <> "" Then
   Do While ReportSize <> "1" And ReportSize <> "2"
 
        ReportSize = InputBox("Please enter the number cooresponding to the paper size " _
        & "you wish to print on." & _
        vbCrLf & "1 - Letter" & _
        vbCrLf & "2 - A4", "Please choose your paper size")
 
    Loop
End If
This would force them into a loop if they entered anything in the input box, but I am not sure why a user would enter anything other than 1 or 2. Can you give them a pop-up modal form with radio buttons and cancel instead?

Did that help?
__________________
mmcdonal

Look it up at: http://wrox.books24x7.com
The Following User Says Thank You to mmcdonal For This Useful Post:
lryckman (April 9th, 2009)
 
Old April 8th, 2009, 08:06 AM
Friend of Wrox
 
Join Date: Mar 2004
Posts: 3,069
Thanks: 0
Thanked 10 Times in 10 Posts
Default

Also, I would do this:

Code:
Dim sMessage As String
Dim sTitle As String
 
sMessage = "Please enter the number cooresponding to the paper size " _
& "you wish to print on." & _
vbCrLf & "1 - Letter" & _
vbCrLf & "2 - A4"
 
sTitle = "Please choose your paper size"
 
DoCmd.RunCommand acCmdSaveRecord
ReportSize = InputBox(sMessage, sTitle)

That way if you have to change the message, you don't have to go hunting for it in the code. You have a misspelling here, so when you have to change it, you will have to change it twice.

HTH
__________________
mmcdonal

Look it up at: http://wrox.books24x7.com
The Following User Says Thank You to mmcdonal For This Useful Post:
lryckman (April 9th, 2009)
 
Old April 8th, 2009, 05:16 PM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 1,151
Thanks: 2
Thanked 14 Times in 14 Posts
Send a message via ICQ to SerranoG Send a message via AIM to SerranoG
Default

How about?

Code:
    Dim strReportSize As String
    Dim strTitle As String, strMessage as String
 
    DoCmd.RunCommand acCmdSaveRecord
 
    strReportSize = "Unknown"
 
    Do While strReportSize <> "1" And strReportSize <> "2" And _
        Len(strReportSize) > 0
 
        strMessage = "Please enter the number corresponding " & _
            " to the paper size you wish to print on." & _
            vbCrLf & vbCrLf & "1 - Letter" & vbCrLf & "2 - A4"
 
        strTitle = "Please choose your paper size."
 
        strReportSize = Trim(InputBox(strMessage, strTitle, 1))
    Loop
 
    Select Case strReportSize
        Case "1"
            OpenReport_FX "rptECNForm", acViewNormal, "", _
                "[CCNNumber] = [Forms]![frm2009_11]![CCNNumber]"
        Case "2"
            OpenReport_FX "rptECNForm_A4", acViewNormal, "", _
                "[CCNNumber]=[Forms]![frm2009_11]![CCNNumber]"
        Case Else
            MsgBox "Operation Canceled.", vbInformation, "Open Report"
    End If
Note: I've not tested this. The indenting will look funny in your e-mail. Look at this on the web to view it in the code box.
__________________
Greg Serrano
Michigan Dept. of Environmental Quality
Air Quality Division
The Following User Says Thank You to SerranoG For This Useful Post:
lryckman (April 9th, 2009)
 
Old April 8th, 2009, 07:31 PM
Authorized User
 
Join Date: Jul 2008
Posts: 38
Thanks: 1
Thanked 2 Times in 2 Posts
Default

If I read this correctly, I am seeing it differently than anyone else. I thought you wanted to let them cancel out if they choose cancel. The simplest way is just to change this part:
Code:
 ReportSize = InputBox("Please enter the number cooresponding to the paper size " _
    & "you wish to print on." & _
    vbCrLf & "1 - Letter" & _
    vbCrLf & "2 - A4", "Please choose your paper size")
to this:
Code:
 ReportSize = InputBox("Please enter the number cooresponding to the paper size " _
    & "you wish to print on." & _
    vbCrLf & "1 - Letter" & _
    vbCrLf & "2 - A4", "Please choose your paper size")
If ReportSize = "" Then
  MsgBox "Selection Canceled", vbInformation, "Cancel"
  Exit Sub
End If
__________________
Thanks,

Bob Larson
Access MVP (2008-2010, 2011)
Free Access tutorials, samples, tools
 
Old April 9th, 2009, 07:19 AM
Friend of Wrox
 
Join Date: Mar 2004
Posts: 3,069
Thanks: 0
Thanked 10 Times in 10 Posts
Default

Yes, that is what I obtained with

If ReportSize <> "" Then
...
End If

The post was to prevent the users from getting into an infinite loop (allowing them to cancel.) There should have been an Exit Sub there.
__________________
mmcdonal

Look it up at: http://wrox.books24x7.com
 
Old April 9th, 2009, 07:44 AM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 1,151
Thanks: 2
Thanked 14 Times in 14 Posts
Send a message via ICQ to SerranoG Send a message via AIM to SerranoG
Default

The code I posted should do the same except I try to avoid Exit Subs as much as possible. I don't like code that just bails out midway. I prefer "if the condition doesn't apply to you, then skip it and continue to the next code, even if that's an End Sub."

Just a different mindset.
__________________
Greg Serrano
Michigan Dept. of Environmental Quality
Air Quality Division
 
Old April 9th, 2009, 08:12 AM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 126
Thanks: 5
Thanked 0 Times in 0 Posts
Default

Everyone's help is much appreciated. This is working correctly now allowing the user to cancel out of the loop.

Thank you!
Laura





Similar Threads
Thread Thread Starter Forum Replies Last Post
Retreiving value from InputBox maniatis1 Classic ASP Professional 1 January 4th, 2007 03:36 PM
Loop issue - with n number of same nodes.. ramesh.kumarm XSLT 3 March 8th, 2006 08:19 AM
Format inputbox jesseleon Access VBA 2 October 5th, 2005 03:14 PM
Nested Loop Select Box Issue mat41 Classic ASP Basics 5 August 24th, 2004 11:02 PM
InputBox Misbehaving bph Access 10 March 4th, 2004 01:09 PM





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