My VB to open a form isn't working

J

John Ortt

I have copied some code from another form which opens a form to display
certain information.

I have changed the relevant bits (I.E module name, form name and field name)
but otherwise left it the same

Still it wont work. I get the following error message:

/////////////////////////////////////
Run-time error '2501':

The OpenForm action was cancelled.

You used a method of the DoCmd object to carry out an action in visual
Basic, but then clicked cancel in a dialogue box.
For example, you used the close method to close a changed form, then clicked
cancel in the dialogue box that asked you if you want to save the changes
you made to the form.
////////////////////////////////////


my new code is as follows:
____________________

Private Sub Supplier_DblClick(Cancel As Integer)
Link
End Sub

Function Link()

Dim stDocName As String
Dim stLinkCriteria As String
Dim stLinkCriteria1 As String

stDocName = "F:partViewerSub"

stLinkCriteria = "[Key]=" & "'" & Me![Key] & "'"

MsgBox (stLinkCriteria)

DoCmd.OpenForm stDocName, , , stLinkCriteria

End Function


The Original (working code) is as follows:
_______________________________

Private Sub CUSTOMER_2_DblClick(Cancel As Integer)
Link
End Sub

Function Link()

Dim stDocName As String
Dim stLinkCriteria As String
Dim stLinkCriteria1 As String
Dim stLinkCriteria2 As String
Dim stLinkCriteria3 As String
Dim stLinkCriteria4 As String

stDocName = "VendorMarginsDDForm"

stLinkCriteria1 = "[TYPE]=" & "'" & Me![TYPE] & "'"
stLinkCriteria2 = "[IPT]=" & "'" & Me![IPT] & "'"
stLinkCriteria3 = "[CUSTOMER_2]=" & "'" & Me![CUSTOMER_2] & "'"
stLinkCriteria4 = "[SD Forecast]=" & "'" & Me![SD Forecast] & "'"

stLinkCriteria = stLinkCriteria1 & " and " & stLinkCriteria2 & " and " &
stLinkCriteria3 & " and " & stLinkCriteria4
docmd.Openform stDocName, , , stLinkCriteria

End Function
 
A

Andi Mayer

DoCmd.OpenForm stDocName, , , stLinkCriteria

Got to this line press F9 (set a break point)

and run your code

with F8 walk through your code step by step and see what is happening
when you open the form
 
E

Eric Schittlipz

John Ortt said:
I have copied some code from another form which opens a form to display
certain information.

I have changed the relevant bits (I.E module name, form name and field
name)
but otherwise left it the same

Still it wont work. I get the following error message:

/////////////////////////////////////
Run-time error '2501':

The OpenForm action was cancelled.

You used a method of the DoCmd object to carry out an action in visual
Basic, but then clicked cancel in a dialogue box.
For example, you used the close method to close a changed form, then
clicked
cancel in the dialogue box that asked you if you want to save the changes
you made to the form.
////////////////////////////////////


my new code is as follows:
____________________

Private Sub Supplier_DblClick(Cancel As Integer)
Link
End Sub

Function Link()

Dim stDocName As String
Dim stLinkCriteria As String
Dim stLinkCriteria1 As String

stDocName = "F:partViewerSub"

stLinkCriteria = "[Key]=" & "'" & Me![Key] & "'"

MsgBox (stLinkCriteria)

DoCmd.OpenForm stDocName, , , stLinkCriteria

End Function


The Original (working code) is as follows:
_______________________________

Private Sub CUSTOMER_2_DblClick(Cancel As Integer)
Link
End Sub

Function Link()

Dim stDocName As String
Dim stLinkCriteria As String
Dim stLinkCriteria1 As String
Dim stLinkCriteria2 As String
Dim stLinkCriteria3 As String
Dim stLinkCriteria4 As String

stDocName = "VendorMarginsDDForm"

stLinkCriteria1 = "[TYPE]=" & "'" & Me![TYPE] & "'"
stLinkCriteria2 = "[IPT]=" & "'" & Me![IPT] & "'"
stLinkCriteria3 = "[CUSTOMER_2]=" & "'" & Me![CUSTOMER_2] & "'"
stLinkCriteria4 = "[SD Forecast]=" & "'" & Me![SD Forecast] & "'"

stLinkCriteria = stLinkCriteria1 & " and " & stLinkCriteria2 & " and " &
stLinkCriteria3 & " and " & stLinkCriteria4
docmd.Openform stDocName, , , stLinkCriteria

End Function


You should look at the form "F:partViewerSub" You will find some code in
there which is cancelling the opening of the form - for example you pass a
value which the form doesn't know what to do with, eg:
DoCmd.OpenForm "F:partViewerSub", , , "Bananas"
The code says "Bananas" means nothing to me so I won't open - however the
error message is reported in the code which asked the form to open.
 
J

John Ortt

I have figured out the problem. It is because I am trying to send the
following as the criteria.

[Key]='3' (I.E. Integer Values)

If I try to send a text value such as [Status]='Cleared' it works perfectly

Any ideas as to how I can make it work with Integers?
 
A

Andi Mayer

I have figured out the problem. It is because I am trying to send the
following as the criteria.

[Key]='3' (I.E. Integer Values)

If I try to send a text value such as [Status]='Cleared' it works perfectly

Any ideas as to how I can make it work with Integers?
leave the quotes out
 
J

John Ortt

Andi Mayer said:
I have figured out the problem. It is because I am trying to send the
following as the criteria.

[Key]='3' (I.E. Integer Values)

If I try to send a text value such as [Status]='Cleared' it works perfectly

Any ideas as to how I can make it work with Integers?
leave the quotes out

Doh!!!......Thanks Andi
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top