Share via

DoCmd.OpenForm

Anonymous
2020-09-09T12:25:08+00:00

I get a Type mismatch error with this code but if I remove frmview from DoCmd then it works fine and form loads in form view.

Dim myform, frmview as string

myform = "Customers"

frmview="acFormDS"

DoCmd.OpenForm myform, frmview

Is it possible to specify acFormDS parameter with a variable in DoCmd ?

Microsoft 365 and Office | Access | For home | Windows

Locked Question. This question was migrated from the Microsoft Support Community. You can vote on whether it's helpful, but you can't add comments or replies or follow the question.

0 comments No comments

Answer accepted by question author

Anonymous
2020-09-09T16:39:00+00:00

The reason why you don't need quotes characters around acFormDS is because it is not a string expression, but a constant.  Its value is an integer number, in this case 3.  You can see this in a couple of ways:

1.  Enter the following in the immediate window:

? acFormDS

 3

2.Open the Object Browser and search for acFormDS.  Down at the bottom of the dialogue you'll see:

Const acFormDS = 3

    Member of Access.AcFormView

So if you wish to assign it to a variable, you could do so like this:

    Dim frmview As Integer

    frmview = acFormDS

but it would be better to do as Tom described:

    Dim frmview As AcFormView

    frmview = acFormDS

either way:

    Debug.Print frmview

would return 3 in the immediate (aka Debug) window.

Was this answer helpful?

1 person found this answer helpful.
0 comments No comments

Answer accepted by question author

Tom van Stiphout 40,201 Reputation points MVP Volunteer Moderator
2020-09-09T14:26:34+00:00

Hans is correct. How did he know that?

The help page for DoCmd.OpenForm is here: 

https://docs.microsoft.com/en-us/office/vba/api/Access.DoCmd.OpenForm

Under "Parameters" it says:

View Optional AcFormView

So you should have written:

Dim frmview as AcFormView

frmview = 

And then Intellisense would have shown you the list of the AcFormView enumeration.

A few more comments:

> Dim myform, frmview as string

What is the datatype of myform? You think string; I know it is variant. You can check with the VarType function.

If you want to declare multiple variables on one line you have to specify the type for each:

Dim myform as string, frmview as AcFormView

>frmview as string

"frm" is a very poor prefix for a string. By convention it refers to a form object:

dim frmCustomer as Form

set frmCustomer = Forms!frmCustomer   'assumes the form is open

Was this answer helpful?

1 person found this answer helpful.
0 comments No comments

2 additional answers

Sort by: Most helpful
  1. Anonymous
    2020-09-14T10:37:14+00:00

    Thank you for helping out.  I have changed the datatype and now all works well.  I would never have figured it out without your help.

    Was this answer helpful?

    0 comments No comments
  2. HansV 462.6K Reputation points
    2020-09-09T14:10:31+00:00

    Remove the quotes from around acFormDS:

    frmview = acFormDS

    Was this answer helpful?

    0 comments No comments