problem to change form title (1 Viewer)

eugzl

Member
Local time
Today, 09:04
Joined
Oct 26, 2021
Messages
125
Hi All.
I would like to open form with according title. For that I create following code:
Code:
Option Compare Database
Option Explicit

Private Sub Form_Open(Cancel As Integer)
    FormArg = Split(Form.OpenArgs, ",")
    Me.Caption = FormArg(1)
End Sub
When I run the form I'm getting error message:
Compile error:
Variable not defined.

I know that causes is Option Explicit. When I comment it the caption works. How to change the code to keep Option Explicit and have ability to modify a form title programmatically?
Thanks.
 
Last edited:

Gasman

Enthusiastic Amateur
Local time
Today, 14:04
Joined
Sep 21, 2011
Messages
14,269
So Dim the variable?
You should do this for ALL variables. :(
You have been here since last October and you do not know this?:(

I would also use Me.OpenArgs, not what you have?
 

eugzl

Member
Local time
Today, 09:04
Joined
Oct 26, 2021
Messages
125
So Dim the variable?
You should do this for ALL variables. :(
You have been here since last October and you do not know this?:(

I would also use Me.OpenArgs, not what you have?
Hi Gasman. Thanks for reply.
When I code like
Code:
Private Sub Form_Open(Cancel As Integer)
    Dim formArg as String
    FormArg = Split(Form.OpenArgs, ",")
    Me.Caption = FormArg(1)
End Sub
I'm getting error message:
Compile error:
Expected array

How to fix the problem?
Thanks
 

LarryE

Active member
Local time
Today, 06:04
Joined
Aug 18, 2021
Messages
589
If you are attempting to simply define the forms Caption property, then you can:
Me.Caption="Type the caption text here"
 

Gasman

Enthusiastic Amateur
Local time
Today, 14:04
Joined
Sep 21, 2011
Messages
14,269
How many parameters are you passing in?
First parameter would be (0)
Google Split()

Code:
Sub testsplit()
Dim strVar1() As String
strVar1 = Split("this is a text", " ")
MsgBox strVar1(3)

End Sub
 
Last edited:

eugzl

Member
Local time
Today, 09:04
Joined
Oct 26, 2021
Messages
125
How many parameters are you passing in?
First parameter would (0)
Google Split()

Code:
Sub testsplit()
Dim strVar1() As String
strVar1 = Split("this is a text", " ")
MsgBox strVar1(3)

End Sub
In the parent form for click button event to create a new record
Code:
FormArg = "0,New Supply"
DoCmd.OpenForm stDocName, acNormal, , , acFormAdd, , FormArg
In the child form I used your suggestion
Code:
Dim formArg() As String
formArg = Split(Form.OpenArgs, ",")
the problem is gone.

But in case when I in double click event in parent form call the child form to modify existing record
Code:
formArg = Me.SupplyName
DoCmd.OpenForm "fSupply", , , "SupplyID = " & Me.SupplyID, , , formArg
in the Form_Open procedure of the child form in the line
Code:
formArg = Split(Form.OpenArgs, ",")
I'm getting error message:
Run-time error'9':
Subscript out of range

How to fixit?
Thanks
 

eugzl

Member
Local time
Today, 09:04
Joined
Oct 26, 2021
Messages
125
Add a comma?
With 2 commas before formArg gave me error:
The expression On Dbl Click you entered as the event ptoperty setting produced the following error: Type mismatch.
 

The_Doc_Man

Immoderate Moderator
Staff member
Local time
Today, 08:04
Joined
Feb 28, 2001
Messages
27,172
At least part of the issue is that SPLIT expects to return an array (using the comma as an array-member delimiter). You appear to be sending it to a string in one case, and that can't be right - unless you only want the 0th element of the implied array. It worked when you DIDN'T DIM the variable because then all "created" variables are VARIANTS. So... explicitly dim the variable as a VARIANT.
 

eugzl

Member
Local time
Today, 09:04
Joined
Oct 26, 2021
Messages
125
At least part of the issue is that SPLIT expects to return an array (using the comma as an array-member delimiter). You appear to be sending it to a string in one case, and that can't be right - unless you only want the 0th element of the implied array. It worked when you DIDN'T DIM the variable because then all "created" variables are VARIANTS. So... explicitly dim the variable as a VARIANT.
I substituted
Dim formArg() as String to Dim formArg() as Variant
now when I call child form to create a new record and when I double click to call child form to modify a record in both cases I have the same error message:
Run-time error'12':
Type mismatch
 

The_Doc_Man

Immoderate Moderator
Staff member
Local time
Today, 08:04
Joined
Feb 28, 2001
Messages
27,172
OK, part of the problem is that Dim formArg() as Variant actually means "make an arbitrary array of variants." But that isn't what I was suggesting. When you use SPLIT to split up a delimited string (i.e. comma-separated), the single Variant variable BECOMES an array that is zero-based. Then you can reference it using array syntax even though it wasn't declared as an array. But the "type mismatch" is going to depend on what is actually passed in via OpenArgs and to what kind of variable was given the extracted sub-string.
 

Gasman

Enthusiastic Amateur
Local time
Today, 14:04
Joined
Sep 21, 2011
Messages
14,269
With 2 commas before formArg gave me error:
The expression On Dbl Click you entered as the event ptoperty setting produced the following error: Type mismatch.
No!
Add a comma to the string that you are splitting. :-(
"The name of my Form today,")
 

eugzl

Member
Local time
Today, 09:04
Joined
Oct 26, 2021
Messages
125
No!
Add a comma to the string that you are splitting. :-(
"The name of my Form today,")
I have comma in the split
In the parent form
Code:
Public Function OpenForm()
    Dim formArg As String
    formArg = Me.SupplyID + "," + Me.Supply

    DoCmd.OpenForm "fSupply", acNormal, , , "SupplyID=" & Me.SupplyID, , formArg
End Function
When I double click in the parent form I'm getting error message
DblClk_ContnsFormErr.png

I don't know what exactly is not correct but I guess the problem in the function OpenForm().
In the child form
Code:
Private Sub Form_Open(Cancel As Integer)
    Dim formArg() As Variant
    formArg = Split(Form.OpenArgs, ",")
    Me.Caption = formArg(1)
End Sub
 
Last edited:

Gasman

Enthusiastic Amateur
Local time
Today, 14:04
Joined
Sep 21, 2011
Messages
14,269
Well look to see what is in the DblClick event of whatever you clicked?
If supplyID is numeric (and anything I do with ID is generally an autonumber), then perhaps use Cstr() to convert it to a string.

I will go to my grave saying this..... :(
Walk through the code line by line with F8 and see where the error occurs
 

Gasman

Enthusiastic Amateur
Local time
Today, 14:04
Joined
Sep 21, 2011
Messages
14,269
Whilst you can use + for strings to concatenate, best to use & unless you need the option + allows.
 

Users who are viewing this thread

Top Bottom