Results 1 to 4 of 4
  1. #1
    Bruce is offline Advanced Beginner
    Windows XP Access 2003
    Join Date
    Nov 2009
    Posts
    72

    VBA Open Form If - Else

    I've got a form with a command button that opens another form and I'm trying to make it open 1 of 2 possible forms (ABCD or EFGH) based on criteria: Here is the code:

    Private Sub Command35_Click()
    On Error GoTo Err_Command35_Click

    If (Field1.Text = "ABCD") Then

    Dim stDocName As String
    Dim stLinkCriteria As String

    stDocName = "ABCD_Form"
    DoCmd.OpenForm stDocName, , , stLinkCriteria
    Else:

    stDocName = "EFGH_Form"
    DoCmd.OpenForm stDocName, , , stLinkCriteria

    End If

    Exit_Command35_Click:
    Exit Sub

    Err_Command35_Click:
    MsgBox Err.Description
    Resume Exit_Command35_Click



    End Sub

    I get an error You can't regerence a property or method for a control unless the control has focus. Any suggestions are appreciated. Tks!

  2. #2
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,530
    Don't use the Text property:

    If (Field1 = "ABCD") Then

    I would add that for efficiency, all I would do in the If/Then block is set the variable:

    stDocName = "ABCD_Form"

    I'd have the OpenForm after, so you only have it once. Also, if you're not using stLinkCriteria I wouldn't include it anywhere.

  3. #3
    maximus's Avatar
    maximus is offline Expert
    Windows 7 Access 2010 (version 14.0)
    Join Date
    Aug 2009
    Location
    India
    Posts
    931
    Yes the problem in the the code is If (Field1.Text = "ABCD") Then Use Me.FieldName="ABCD" then.

  4. #4
    Bruce is offline Advanced Beginner
    Windows XP Access 2003
    Join Date
    Nov 2009
    Posts
    72
    This worked. Thanks!

    If (Field1 = "ABCD") Then
    Dim stDocName As String

    stDocName = "ABCD_Form"
    DoCmd.OpenForm stDocName

    Else:

    stDocName = "EFGH_Form"
    DoCmd.OpenForm stDocName
    End If

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

Similar Threads

  1. How to open a blank form?
    By WhatnThe in forum Access
    Replies: 15
    Last Post: 08-05-2011, 02:41 PM
  2. Open a form on 90% Zoom
    By rhutton7 in forum Forms
    Replies: 0
    Last Post: 07-01-2009, 09:53 PM
  3. Open to specific form
    By ecpike in forum Forms
    Replies: 2
    Last Post: 06-12-2009, 08:32 AM
  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