Results 1 to 10 of 10
  1. #1
    pm4698 is offline Novice
    Windows XP Access 2007
    Join Date
    Mar 2010
    Posts
    5

    Open form - menu

    Hello there,



    I want to click a button and appear a msgBox from which i will have 4 options(each option with a label and a checkbox).
    Each option corresponds to a different form name.
    When i will choose an option (checkbox) then the selected form's name will be saved at a string variable. Then i will use this string to open the selected form with code:
    docmd.openform "string_variable".

    A piece of code that i tried to write is:

    'public strInput As String 'Is used to save the form's name
    Private button_name_Click()


    Dim strInput1 As CheckBox 'Choose frm1
    Dim strInput2 As CheckBox 'Choose frm2
    Dim strInput3 As CheckBox 'Choose frm3
    Dim strInput4 As CheckBox 'Choose frm4

    Dim strMsg As String

    Beep
    strMsg = "Please choose which form to open..."
    MsgBox "Form1" & vbCrLf & vbLf & strInput1 & _
    "Form2" & vbCrLf & vbLf & strInput2 & _
    "Form3" & vbCrLf & vbLf & strInput3 & _
    "Form4" & vbCrLf & vbLf & strInput4 & _
    "", _
    vbInformation, "Choose form..."

    strInput = InputBox(Prompt:=strMsg, title:="Choose form")

    if me.strInput1.value= true then
    strInput = "frm1 & "'"
    else if me.strInput2.value= true then
    strInput = "frm2 & "'"
    else if me.strInput3.value= true then
    strInput = "frm3 & "'"
    else if me.strInput4.value= true then
    strInput = "frm4 & "'"
    end if
    end if
    end if
    end if

    end sub

    So, when i choose a form from the msgbox, the name of the form that i chose, is saved to strInput (for example frm1).

    Then to open the form1 for example i will write:
    DoCmd.OpenForm "strInput", , , , , ,

    Can i do this?
    If yes, could you give me some corrections to the code?

    Thanks in advance

  2. #2
    maximus's Avatar
    maximus is offline Expert
    Windows 7 Access 2010 (version 14.0)
    Join Date
    Aug 2009
    Location
    India
    Posts
    931
    Your code as many errors
    1) if elseif block not proper
    2) strInput Not Defined
    3) As strInput not defined how can you set value of strInput1 to true
    4) String formatting in strInput in the last block of your code.


    I have modified your code. put this code on the OnClick event of Command Button of a form. The user will be show a message and will be instructed to type integers from 1-4 for forms e.g. for Form1 type 1 one number id typed Form 1 will open.



    Dim intFormNumber As Integer
    Dim strFormName As String

    MsgBox "For Form1 Type 1" & vbCrLf & vbLf & strInput1 & _
    "For Form2 Type 2" & vbCrLf & vbLf & strInput2 & _
    "For Form3 Type 3" & vbCrLf & vbLf & strInput3 & _
    "For Form4 Type 4" & vbCrLf & vbLf & strInput4 & _
    "", _
    vbInformation, "Choose form..."

    intFormNumber = InputBox("Please type a valid integer From 1-4 as Form Index")
    strFormName = "Form" & intFormNumber

    DoCmd.OpenForm strFormName

    if this solves you problem mark this thread solved.

  3. #3
    maximus's Avatar
    maximus is offline Expert
    Windows 7 Access 2010 (version 14.0)
    Join Date
    Aug 2009
    Location
    India
    Posts
    931
    have you been able to solve your problem, if yes mark the thread solved.

  4. #4
    pm4698 is offline Novice
    Windows XP Access 2007
    Join Date
    Mar 2010
    Posts
    5
    My forms' names have no numbers.
    I have MON, TUS, WED, THU as names of 4 forms.

    If i got it right, your code works with numbers.
    What about texts?

    Thank you in advance

  5. #5
    maximus's Avatar
    maximus is offline Expert
    Windows 7 Access 2010 (version 14.0)
    Join Date
    Aug 2009
    Location
    India
    Posts
    931
    Well here are two possible solutions:

    The First Code uses Form Index Like 1 for Mon, 2 for Tus etc. I have use the same setup just included a Select Case block to Select the correct from which needs to be open.


    Dim intFormNumber As Integer
    Dim strFormName As String


    MsgBox "For MON Type 1" & vbCrLf & vbLf & strInput1 & _
    "For TUS Type 2" & vbCrLf & vbLf & strInput2 & _
    "For WED Type 3" & vbCrLf & vbLf & strInput3 & _
    "For THU Type 4" & vbCrLf & vbLf & strInput4 & _
    "", _
    vbInformation, "Choose form..."

    intFormNumber = InputBox("Please type a valid integer From 1-4 as Form Index")

    Select Case intFormNumber

    Case Is = 1
    strFormName = "MON"
    Case Is = 2
    strFormName = "TUS"
    Case Is = 3
    strFormName = "WED"
    Case Is = 4
    strFormName = "THU"
    End Select

    DoCmd.OpenForm strFormName

    In this code I have made provision that the name of the Form is directly typed in the input box and response to which the form is opened. e.g. type Mon and Form Mon will open.

    Dim strFormName As String
    MsgBox "For MON Type 1" & vbCrLf & vbLf & strInput1 & _
    "For TUS Type Tus" & vbCrLf & vbLf & strInput2 & _
    "For WED Type WED" & vbCrLf & vbLf & strInput3 & _
    "For THU Type THU" & vbCrLf & vbLf & strInput4 & _
    "", _
    vbInformation, "Choose form..."

    strFormName = InputBox("Please type a valid Form Name")
    DoCmd.OpenForm strFormName


    I personally prefer the first code as that involves the user typing numbers instead of words. The problem with the second code is typos e.g is you type ts, tues you will get an error.


    I was surprised that you didnot modify the code yourself. Play with codes you will learn more.
    If this solves your problem mark the thread solved.

  6. #6
    pm4698 is offline Novice
    Windows XP Access 2007
    Join Date
    Mar 2010
    Posts
    5
    The first code works just fine to me!

    Thank you very much!

    I have a last question.
    At the same form i have a vba code at after update of a combo box. So, all i want is the value that is saved to the strFormName variable to be able to use it to the public funtion of the after update of the combo box.
    At the after update of the combo box i want to use a code like this:
    DoCmd.Openform strFormName, , , , , , me.textbox & ";" & me.combobox

    but when i tried it, i got a message that i have to declare the name of the form and the strFormName could not be recognized as the name of the form of the first function

  7. #7
    maximus's Avatar
    maximus is offline Expert
    Windows 7 Access 2010 (version 14.0)
    Join Date
    Aug 2009
    Location
    India
    Posts
    931

    reusing variable values

    The code is divided into three parts. Now your problem is You want the value of a variable declared once in a code to be re-used.

    Observe Carefully. I have made a General Declaration of strGeneralFormName. Now I will assign a value to this variable using my first code which is attached to a command button used to select a Form. Once the variable has been assigned a value I will use the value again in the AfterUpdate Even of Combo2 with re-assigning any value to it.

    Scope of the Code:


    I have a Forms MON,TUS,WED,THU and another Form FriMON

    Now I want to open FriMON using the AfterUpdate Event of the Combo2. My Combo2 has the Value Fri I want to combine it with MON that I had selected when I ran the Code to open Form MON. The value thus assigned to strGeneralFormName can be reused as I have done so combining it with the value of the Combo2 to Generate the name of the form FriMON.

    Please mark the thread solved if your problem is solved. By the way the code that you posted I could understand that you wanted to open a form have you included an argument to it if yes please check it for errors.


    Option Compare Database
    Dim strGeneralFormName As String


    Private Sub Combo2_AfterUpdate()
    Dim strMyForm As String
    strMyForm = Me.Combo2 & strGeneralFormName
    DoCmd.OpenForm strMyForm
    End Sub

    Private Sub Command1_Click()
    On Error GoTo Err_Command1_Click
    Dim intFormNumber As Integer
    MsgBox "For MON Type 1" & vbCrLf & vbLf & strInput1 & _
    "For TUS Type 2" & vbCrLf & vbLf & strInput2 & _
    "For WED Type 3" & vbCrLf & vbLf & strInput3 & _
    "For THU Type 4" & vbCrLf & vbLf & strInput4 & _
    "", _
    vbInformation, "Choose form..."

    intFormNumber = InputBox("Please type a valid integer From 1-4 as Form Index")

    Select Case intFormNumber

    Case Is = 1
    strGeneralFormName = "MON"
    Case Is = 2
    strGeneralFormName = "TUS"
    Case Is = 3
    strGeneralFormName = "WED"
    Case Is = 4
    strGeneralFormName = "THU"
    End Select

    DoCmd.OpenForm strGeneralFormName
    Exit_Command1_Click:
    Exit Sub

    Err_Command1_Click:
    MsgBox Err.Description
    Resume Exit_Command1_Click

    End Sub

  8. #8
    maximus's Avatar
    maximus is offline Expert
    Windows 7 Access 2010 (version 14.0)
    Join Date
    Aug 2009
    Location
    India
    Posts
    931
    is your problem solved

  9. #9
    pm4698 is offline Novice
    Windows XP Access 2007
    Join Date
    Mar 2010
    Posts
    5
    I am really sorry but i think that i mentioned my problem wrong.
    I already have my vba code at the after update combobox and at a code line i use this:
    DoCmd.Openform strFormName, , , , , , me.textbox & ";" & me.combobox

    I do not want this line of code to be changed.
    The values of the combobox are not related with which form will open.

    Assume that i have 4 columns. At the top of the column i have a button and the rest of each column consists of comboboxes.

    I want the user to firstly push the button at the top of a column, to select which form to use and then, the code with which i am going to open the selected form will be
    DoCmd.Openform strFormName, , , , , , me.textbox & ";" & me.combobox

    and it will open at the line of code that i have placed the code:
    DoCmd.Openform strFormName, , , , , , me.textbox & ";" & me.combobox

  10. #10
    maximus's Avatar
    maximus is offline Expert
    Windows 7 Access 2010 (version 14.0)
    Join Date
    Aug 2009
    Location
    India
    Posts
    931
    Using this part of the code to assign the form name to the vaiable strGeneralFormName

    Private Sub Command1_Click()
    On Error GoTo Err_Command1_Click
    Dim intFormNumber As Integer
    MsgBox "For MON Type 1" & vbCrLf & vbLf & strInput1 & _
    "For TUS Type 2" & vbCrLf & vbLf & strInput2 & _
    "For WED Type 3" & vbCrLf & vbLf & strInput3 & _
    "For THU Type 4" & vbCrLf & vbLf & strInput4 & _
    "", _
    vbInformation, "Choose form..."

    intFormNumber = InputBox("Please type a valid integer From 1-4 as Form Index")

    Select Case intFormNumber

    Case Is = 1
    strGeneralFormName = "MON"
    Case Is = 2
    strGeneralFormName = "TUS"
    Case Is = 3
    strGeneralFormName = "WED"
    Case Is = 4
    strGeneralFormName = "THU"
    End Select


    Exit_Command1_Click:
    Exit Sub

    Err_Command1_Click:
    MsgBox Err.Description
    Resume Exit_Command1_Click

    End Sub

    Now your code in the AfterUpdate Even of Your ComboBox. Only change is using strGeneralFormName instead strFormName
    DoCmd.Openform strGeneralFormName, , , , , , me.textbox & ";" & me.combobox

    mark this thread solved if this solves your problem

Please reply to this thread with any new information or opinions.

Similar Threads

  1. Replies: 1
    Last Post: 03-03-2010, 07:29 PM
  2. Replies: 2
    Last Post: 02-26-2010, 08:14 AM
  3. VBA Open Form If - Else
    By Bruce in forum Forms
    Replies: 3
    Last Post: 02-11-2010, 12:57 PM
  4. In a field on a Form, on click open another form
    By jackieagra in forum Programming
    Replies: 1
    Last Post: 03-20-2008, 09:44 AM
  5. Form will not open
    By dserbanescu in forum Forms
    Replies: 0
    Last Post: 01-09-2008, 09:48 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Other Forums: Microsoft Office Forums