How to set a variable in a form from another form

catbeasy

Registered User.
Local time
Today, 00:59
Joined
Feb 11, 2009
Messages
140
I have code that I would like to set a variable in an open form from another open form. I get an error when using the code pasted below:

"Application Defined or Object Defined Error.."

Can someone tell me if you can set a variable dim'd in another form and if so what the syntax is?

Thanks..
=====================================
This part of the code is where it is erring out:

frm.str_entry_type = str_fra_val_proc_frm

I want the variable that is dim'd in the frm_pr_proc_sel_req to be set to the value of the variable in the current open form (named frm_entry_type)

=====================================
CODE:

Dim frm As Form
Set frm = Forms("frm_pr_proc_sel_req")
Dim str_msg_1 As String
Dim str_msg_2 As String
Dim str_fra_val As String
Dim str_fra_val_proc_frm As String 'for use in setting the value of the form: frm_pr_proc_sel_req varible so can decide if requester wants se or pr record..

If Me.fra_entry_type.Value = 1 Then
str_fra_val = "Profile Entry"
str_fra_val_proc_frm = "pr"
ElseIf Me.fra_entry_type.Value = 2 Then
str_fra_val = "Special Exception Entry"
str_fra_val_proc_frm = "se"
End If

str_msg_1 = "You have selected " & vbCrLf & vbCrLf & UCase(str_fra_val) & vbCrLf & vbCrLf _
& "as the Entry Type. If This is correct, click YES. Otherwise click NO and reselect"

str_msg_2 = "Please re-enter a new entry type"

If fcn_confirm(str_msg_1) = True Then
frm.str_entry_type = str_fra_val_proc_frm
DoCmd.Close
Else
MsgBox str_msg_2
Me.fra_entry_type = Null
End If
 
Better (I believe) to use a public variable from a standard module. Create your variable as public in the standard module (not form module) up at the top just after the
Option Compare Database
Option Explicit (if you have that part too)

Public str_fra_val_proc_frm As String

and then you can refer to it from both forms.
 
Thanks, that works.

Also I think I'm having an issue with code placement with this. The form that has the variable I want set opens the form that sets the variable.

However, the next line of code is supposed to continue based on the decision made and selected in the newly opened form. In other words, I think that the code opens the form and then continues to run the code underneath before any decision is made in the newly opened form. Any ideas on how to get around this?

so, below, it opens the form "Entry Type". What I want to happen is select an entry type and then the (now public!) variable str_entry_type in the form frm_pr_proc_sel_rec (which has the below code) gets the value "pr" or "se" and then the IF/Elseif statment codes are run accordingly..

====================================================

If fcn_confirm(str_msg) = True Then 'make visible & lock profile, req type controls & open profile select form

DoCmd.OpenForm "frm_entry_type"

If str_entry_type = "pr" Then

Call sub_ctl_visibility_pr_req_true

Me.txt_pr_code_desc.Locked = True
Me.txt_req_type_desc.Locked = True
Me.txt_assoc_id.SetFocus
Me.cmd_confirm.Visible = False

'if profile has already been filled out, then go directly to open request type form..
If IsNull(Me.txt_pr_code_desc) Then
DoCmd.OpenForm "frm_pr_code_sel"
Else
DoCmd.OpenForm "frm_req_type_sel"
End If

ElseIf str_entry_type = "se" Then

Me.txt_req_type_desc.Visible = True
Me.lbl_req_type_desc.Visible = True
Me.txt_req_type_desc.Locked = True

Me.lbl_se_ident_desc.Visible = True
Me.txt_se_ident_desc.Visible = True
Me.txt_se_ident_desc.Locked = True

Me.txt_assoc_id.SetFocus
Me.cmd_confirm.Visible = False

If IsNull(Me.txt_se_ident_desc) Then
DoCmd.OpenForm "frm_se_code_sel"
Else
DoCmd.OpenForm "frm_req_type_sel"
End If


end if
 
Try changing the line:

DoCmd.OpenForm "frm_entry_type"

to this:

DoCmd.OpenForm "frm_entry_type",acNormal,,,,acDialog
 
thumbsup.png
 

Users who are viewing this thread

Back
Top Bottom