473,396 Members | 2,158 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,396 software developers and data experts.

Filling unbound form with for-next loop

I am writing a function to fill in the data in an unbound form.

I have a table with field names of 1, 2, 3 etc up to approx 100. I have
an unbound form with fields called 1, 2 3 etc.

I gave everything names like this because I wanted to simplify the
function which fills the unbound form with data. So:

forms![myForm]![1] = rs![1]

works fine, similarly with 2 and so on. But there are 100 fields so I
want to wrap the whole thing in a loop, like

for i = 1 to 100
forms![myform]![i] = rs![i]
next i

but this doesn't work.

Similarly with the field labels, I have

forms![myform]![label1] = getlabel(1) where getlabel() is a little
function that returns a text string.

Can anyone help me with the syntax to get this right so that I can open
the unbound form, and fill in all 100 field labels and field data with
just a few lines of code

Thanks
Dave

Sep 14 '06 #1
6 3403
On 14 Sep 2006 07:13:09 -0700, "Dave G @ K2"
<Da*************@gmail.comwrote:

for i = 1 to 100
forms![myform].Controls(i) = rs![i]
next i

-Tom.
>I am writing a function to fill in the data in an unbound form.

I have a table with field names of 1, 2, 3 etc up to approx 100. I have
an unbound form with fields called 1, 2 3 etc.

I gave everything names like this because I wanted to simplify the
function which fills the unbound form with data. So:

forms![myForm]![1] = rs![1]

works fine, similarly with 2 and so on. But there are 100 fields so I
want to wrap the whole thing in a loop, like

for i = 1 to 100
forms![myform]![i] = rs![i]
next i

but this doesn't work.

Similarly with the field labels, I have

forms![myform]![label1] = getlabel(1) where getlabel() is a little
function that returns a text string.

Can anyone help me with the syntax to get this right so that I can open
the unbound form, and fill in all 100 field labels and field data with
just a few lines of code

Thanks
Dave
Sep 14 '06 #2
Thanks Tom

It didn't work for the field value, but curiously I got it to work for
the labels. Here's a snippet of the code that I'm testing for 2 fields

For i = 1 To 2
Forms![frmCADI].Controls("lbl" & CStr(i)).Caption =
GetCADIText(i)
Forms![frmCADI].Controls(i) = rs![i]
Next i

So the fields are called 1 and 2 and the associated field labels are
called lbl1 and lbl2. If I comment out the second line then it works
fine - I see 2 field labels as expected. But with the second line in it
errors with a 3265 - item not found in this collection.

If I use:
For i = 1 To 2
Forms![frmCADI].Controls("lbl" & CStr(i)).Caption =
GetCADIText(i)
Next i
Forms![frmCADI]![1] = rs![1]
Forms![frmCADI]![2] = rs![2]

then it works great !!

Any ideas
Dave

Sep 14 '06 #3
DaveG,
There's a very easy way to do this that doesn't require the user to
rename all table columns and form fields to numbers as you have done.
All you have to do is to make sure that each field name on the foem
matches the column name in the table.
lq
Dim cmd As ADODB.command
Dim prm As ADODB.Parameter
Dim RS As ADODB.Recordset
Dim ctl As Control
Dim myID As Integer
Dim myFrm as String

Set cmd = New ADODB.command
Set cmd.ActiveConnection = CurrentProject.Connection
cmd.CommandTimeout = 0
cmd.CommandText = "StoredProcedureName"
cmd.CommandType = adCmdStoredProc

myFrm = "myFormName"
myID = Forms(myFrm).UniqueRecordID

Set prm = cmd.CreateParameter("myParameterName", adInteger,
adParamInput, , myID)
cmd.Parameters.Append prm

Set RS = cmd.Execute()

If Not RS.EOF Then
For Each ctl In Forms(myFrm).Controls
If ctl.ControlType = acTextBox Then ctl.value =
RS(ctl.Name)
Next ctl
End If
RS.Close
Set RS = Nothing
Set prm = Nothing
Set cmd = Nothing
Set ctl = Nothing



Dave G @ K2 wrote:
Thanks Tom

It didn't work for the field value, but curiously I got it to work for
the labels. Here's a snippet of the code that I'm testing for 2 fields

For i = 1 To 2
Forms![frmCADI].Controls("lbl" & CStr(i)).Caption =
GetCADIText(i)
Forms![frmCADI].Controls(i) = rs![i]
Next i

So the fields are called 1 and 2 and the associated field labels are
called lbl1 and lbl2. If I comment out the second line then it works
fine - I see 2 field labels as expected. But with the second line in it
errors with a 3265 - item not found in this collection.

If I use:
For i = 1 To 2
Forms![frmCADI].Controls("lbl" & CStr(i)).Caption =
GetCADIText(i)
Next i
Forms![frmCADI]![1] = rs![1]
Forms![frmCADI]![2] = rs![2]

then it works great !!

Any ideas
Dave
Sep 14 '06 #4


Dave,

Did you ever get your code to solve the issue you had populating form
fields? I ask because I have what I would call a similar issue with a
project here.

What I would like to do is write a For .. Next loop to fetch field
values in a form, then have the ability to restore those values after a
subsequent clearing of the form. Almost like a COPY/PASTE. For small
numbers of fields I've done this with a subform of hidden fields that I
shuffle field data to with a macro. Then another macro shuffles the
field data back to the main form.

trying to set a variable to a form field works fine as you know... e.g.:

box(1)= [forms]![form1]![bx1]

But in a loop (I have 80 fields!), I don't know how to specify a
"replaceable parameter" for the form field name. The below code will not
store field data from [bx1] into box(1), it will try to store [b] in
box(1)and fail because there is no field named b of course. Basically
what I need to know is how to specify the interpreted value of b as the
form field name rather than the literal b as used in the assignment
statement.

For a = 1 To 80
b = "bx" & a
box(a) = [Forms]![form1]![b]
Next

Any advice apprectiated.

Rick

*** Sent via Developersdex http://www.developersdex.com ***
Sep 19 '06 #5


Dave,

Did you ever get your code to solve the issue you had populating form
fields? I ask because I have what I would call a similar issue with a
project here.

What I would like to do is write a For .. Next loop to fetch field
values in a form, then have the ability to restore those values after a
subsequent clearing of the form. Almost like a COPY/PASTE. For small
numbers of fields I've done this with a subform of hidden fields that I
shuffle field data to with a macro. Then another macro shuffles the
field data back to the main form.

trying to set a variable to a form field works fine as you know... e.g.:

box(1)= [forms]![form1]![bx1]

But in a loop (I have 80 fields!), I don't know how to specify a
"replaceable parameter" for the form field name. The below code will not
store field data from [bx1] into box(1), it will try to store [b] in
box(1)and fail because there is no field named b of course. Basically
what I need to know is how to specify the interpreted value of b as the
form field name rather than the literal b as used in the assignment
statement.

For a = 1 To 80
b = "bx" & a
box(a) = [Forms]![form1]![b]
Next

Any advice apprectiated.

Rick

*** Sent via Developersdex http://www.developersdex.com ***
Sep 19 '06 #6
I found the solution based on hints from related posts (thanks Tom). To
get and put the contents of the fields in a form, I used the code below.
GetFields will take the contents of fields named BX1, BX2, BX3, etc. and
store them to array box(). This data can subsequently be restored after
alteration to the contents of the form fields with the PutFields code.

Public Sub Form_Load()
ReDim box(80)
------------------------------------------------------
End Sub
Public Sub GetFields()
For a = 1 To 80
b = "bx" & a
box(a) = [Forms]![form1].Controls(b)
Next
End Sub
------------------------------------------------------
Public Sub PutFields()
For a = 1 To 80
b = "bx" & a
[Forms]![form1].Controls(b) = box(a)
Next
End Sub

Rick

*** Sent via Developersdex http://www.developersdex.com ***
Sep 20 '06 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Dave | last post by:
Hello, I've been waisting tons of time on this, so I'll just ask and hopefully someone will be able to help. I am opening an excel document and placing values into it's "letterhead" section...
3
by: Dos Lil | last post by:
I have a unbound field in the subform(for calculating the datediff) which has the control property ==DateDiff("n",,). I have another unbound field in the main form which is for displaying the...
3
by: Pat | last post by:
Hello, I've used Sum() to total bound fields on a continuous form with no problem. However, I now have a continuous form, on which I use an unbound field to calculate the number of hours between...
5
by: -elniniol999- | last post by:
I am DESPERATE for a quick reply.. have exhausted my options... i have a table called order details which contains: order id product id product name quantity unit price sale price
4
by: Lumpierbritches | last post by:
Thank you once again for any and all assistance. I'm building an application that's getting quite bulky due to the number of forms, macros and procedures. I was wondering if there's a way to use 1...
3
by: MLH | last post by:
I have a form, bound to a query. Its RecordSource property is a query named frmEnterLienAmounts. The form has a few bound controls and some unbound controls. The unbound controls are calculated...
10
by: Matthew Wells | last post by:
Hello. I've converted a bound Access 2000 form which displays data retrieved from an Access 2000 database to an unbound form. Now my hyperlinks don't work. I'm assuming it's because the form...
18
by: TORQUE | last post by:
Hi, Im wondering if anyone can help me with a problem. I have a form with more than 50 unbound fields. Some of the fields will be blank from time to time. This seems to be where im having...
11
by: TD | last post by:
I'm looking for input into my decision to switch to ADO and unbound forms. I get tired of having to block all of the ways a user can unknowingly save a record, like using the PageUp, PageDown keys...
1
by: MyWaterloo | last post by:
I have an unbound text box on my main form that is used to show the sum totals in my sub form. I also have a field on my main form that has a control source in a table. This bound field needs to...
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
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
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
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.