473,394 Members | 1,706 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

Using a string name in Forms!____!Text.Value

Dpot
30
I'm working on a database that was created by someone else. They used a code to open a specific form based on the value of a combobox. The code is:
Expand|Select|Wrap|Line Numbers
  1. Dim str Form As String
  2. If Me.ComboSelect.Value = "A" Then
  3. strForm = "AForm"
  4. ElseIf Me.ComboSelect.Value = "B" Then
  5. strForm = "BForm"
  6. ElseIf Me.ComboSelect.Value = "C" Then
  7. strForm = "CForm"
  8. ElseIf Me.ComboSelect.Value = "D" Then
  9. strForm = "DForm"
  10. End If
  11. DoCmd.OpenForm strForm
  12.  
The actual code is quite long.


The problem I'm running into is the form has some text boxes where I would like the values to carry over to the appropriate form that opens.

Right now I have each form individually listed out like this:
Expand|Select|Wrap|Line Numbers
  1. If Me.ComboSelect.Value = "A" Then
  2. Forms!AForm.Text1.Value = Me.Text1.Value
  3. Forms!AForm.Text2.Value = Me.Text2.Value
  4. Forms!AForm.Text3.Value = Me.Text3.Value
  5. End If 
  6.  
And it will open the appropriate form with the text boxes filled in which is wonderful BUT since there are so many options I just wanted to simplify the code to read something like:
Expand|Select|Wrap|Line Numbers
  1. DoCmd.OpenForm strForm
  2. Forms!strForm.Text1.Value = Me.Text1.Value
  3. Forms!strFormText2.Value = Me.Text2.Value
  4. Forms!strForm.Text3.Value = Me.Text3.Value
  5.  
But I am getting "Run-time error '2494': The action or method requires a Form Name argument.
Feb 9 '15 #1

✓ answered by jforbes

Right, you'll either need to create a Form Variable and set it to the Form Instance and then set the values of the controls or use the Forms() or AllForms() collection to get a reference to the Instance of the Form and then set the values.
...I know that sentence was as painful to write as to read.

There are a couple collections available to you. The Forms() and AllForms() collections have lists of Forms. The difference is that AllForms() contains all the Forms in your database regardless if they are opened or not. The Forms() collection contains only opened Forms. So you can refer to the Forms in the collections a few different ways. These examples aren't great, but they give an idea of the different ways to address Forms in the Forms() and AllForms() collections.

So by using the Forms("formname") syntax, you can refer to the Form by name, which could be a string variable like strForm which is set in the large IF..THEN..ELSEIF block (which you might want to convert to a SELECT CASE)

Once the String is set,
Expand|Select|Wrap|Line Numbers
  1. Forms(strForm).Text1.Value = Me.Text1.Value
  2.  
should work for you.

4 2411
jforbes
1,107 Expert 1GB
You could refer to a Form like this:
Expand|Select|Wrap|Line Numbers
  1. Forms(strForm).Text2.Value = Me.Text2.Value
I also use this code to make sure a Form is loaded before I try something like this:
Expand|Select|Wrap|Line Numbers
  1. Function isLoaded(ByRef sFormName As String) As Boolean
  2.     ' Determines if a Form is loaded
  3.     Dim i As Integer
  4.  
  5.     isLoaded = False
  6.     For i = 0 To Forms.Count - 1
  7.         If Forms(i).FormName = sFormName Then
  8.             isLoaded = True
  9.             Exit Function
  10.         End If
  11.     Next
  12. End Function
Feb 9 '15 #2
Dpot
30
Still getting the run-time error. Could it be because
Expand|Select|Wrap|Line Numbers
  1. Dim strForm As String
It highlights the strForm in
Expand|Select|Wrap|Line Numbers
  1. Forms!strForm.Text1.Value = Me.Text1.Value
or
Expand|Select|Wrap|Line Numbers
  1. Forms(strForm).Text1.Value = Me.Text1.Value
Is there another way to write this so that way it would work for both codes?

If I change it to
Expand|Select|Wrap|Line Numbers
  1. Dim strForm As Form
I get "Invalid use of property" and it'll highlight the strForm in this code

Expand|Select|Wrap|Line Numbers
  1. If Me.ComboSelect.Value = "A" Then
  2. strForm = "AForm"
Feb 9 '15 #3
jforbes
1,107 Expert 1GB
Right, you'll either need to create a Form Variable and set it to the Form Instance and then set the values of the controls or use the Forms() or AllForms() collection to get a reference to the Instance of the Form and then set the values.
...I know that sentence was as painful to write as to read.

There are a couple collections available to you. The Forms() and AllForms() collections have lists of Forms. The difference is that AllForms() contains all the Forms in your database regardless if they are opened or not. The Forms() collection contains only opened Forms. So you can refer to the Forms in the collections a few different ways. These examples aren't great, but they give an idea of the different ways to address Forms in the Forms() and AllForms() collections.

So by using the Forms("formname") syntax, you can refer to the Form by name, which could be a string variable like strForm which is set in the large IF..THEN..ELSEIF block (which you might want to convert to a SELECT CASE)

Once the String is set,
Expand|Select|Wrap|Line Numbers
  1. Forms(strForm).Text1.Value = Me.Text1.Value
  2.  
should work for you.
Feb 9 '15 #4
Dpot
30
Thank you so much for your help!
Feb 9 '15 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: Himaxda0 | last post by:
Is it possible to access Objects like (document.form1.textfield1) with a string variable like (var obName='textfield1') and using (document.form1.obName.value)? Ive tried it in IE but the value in...
7
by: mcdonamw | last post by:
This may sound like a stupid stupid question and I figure it would b more "general" than pertaining to a specific Language. I'm using vb.net and I have a bunch of Const values in my program. can...
1
by: Matt | last post by:
In test() method: var path="C:\test\hello.txt"; //returns -1 for path.lastIndexOf("\\"). why?? var pos=path.lastIndexOf("\\"); //return -1 But in showFile() method: We are able to get the...
4
by: Sen | last post by:
This is the Javascript function: function auto_submit(form_name, value) { document.form_name.elements.click(); } This is the XHTML: <select name="category"...
0
by: scott | last post by:
Below, I'm trying to remove the querystring name& value of "catID=12". I mananged to isolate the RESULTS part, but I need to be able to strip the querystring name and it's value, no matter if the...
9
by: Joel Finkel | last post by:
Is there a way to execute a method if all we know is its name as a string? Let's say we have the following class. What is the code for the Execute method? I need a solution that works with the...
1
by: Oleg.Ogurok | last post by:
Hi there, In an XSLT stylesheet, I need to get a value of an attribute. The problem is that I need to generate the name of the attribute on the fly. Is it possible to get the value of an...
6
by: Rico | last post by:
Hello, I'm looking for a way to reference the string name of an enumerator. I know in VB.net you can do "MyEnum.ToString" and get the name instead of the integer value. Is there a way I can do...
4
by: Dev | last post by:
Can replacing this with stringbuilder advisable. Or since iteration is less only 20 times, the time taken by using strings and string builder will be the same. for( int i = 0; i< 20; i++ ) { ...
1
by: oavs | last post by:
Hello, I came across this forum looking for an answer and by reading I found that it seemed really supportive forum. I hope I am posing to correct tread :-( I need to use the option name as...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.