473,399 Members | 3,302 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,399 software developers and data experts.

Loop to get the value of the text box

115 100+
I wonder
If i have 12 unbound text box
and i want to take the value of those text box.
those text box are named (article1, article2, article3,...,article12)
So i write this:
Expand|Select|Wrap|Line Numbers
  1.   Article1=me.article1.value
  2.   Article2=me.article2.value 
  3.   ..........
  4.   Aticle12=me.article12.value
  5.  
Is it possible do this in a for loop or something like that?
Expand|Select|Wrap|Line Numbers
  1. for i=1 to i=12 
  2. Article1 = article& i &.value
  3. next i 
  4.  
I tried this one but it wont work !
Thanks for advance
Aug 17 '10 #1

✓ answered by Stewart Ross

Use the Controls collection to refer to controls on a form by a computed name. Assuming your textboxes contain whole numbers:

Expand|Select|Wrap|Line Numbers
  1. Dim lngTotal as Long
  2. Dim intCtrlNo as Integer
  3. Const cCtrlName = "Article"
  4. for intCtrlNo = 1 to 12
  5.   lngTotal = lngTotal + Clng(Nz(Me.Controls(cCtrlName & intCtrlNo), 0)) 
  6. Next intCtrlNo
The CLng(Nz()) part is to prevent errors arising from null values in your textboxes, which would otherwise prevent the addition from returning the correct result.

-Stewart

7 5080
Stewart Ross
2,545 Expert Mod 2GB
Use the Controls collection to refer to controls on a form by a computed name. Assuming your textboxes contain whole numbers:

Expand|Select|Wrap|Line Numbers
  1. Dim lngTotal as Long
  2. Dim intCtrlNo as Integer
  3. Const cCtrlName = "Article"
  4. for intCtrlNo = 1 to 12
  5.   lngTotal = lngTotal + Clng(Nz(Me.Controls(cCtrlName & intCtrlNo), 0)) 
  6. Next intCtrlNo
The CLng(Nz()) part is to prevent errors arising from null values in your textboxes, which would otherwise prevent the addition from returning the correct result.

-Stewart
Aug 17 '10 #2
hannoudw
115 100+
And what if we have a text box that contain a String value??
could we use this ?
Expand|Select|Wrap|Line Numbers
  1. Dim TypeVal As String
  2. Dim intCtrlNo As Integer
  3. Const cCtrlType = "Type"
  4. For intCtrlNo = 1 To 12
  5. TypeVal = CStr(Nz(Me.Controls(cCtrlType & intCtrlNo, 0)))
  6. Next intCtrlNo
  7.  
Aug 17 '10 #3
Stewart Ross
2,545 Expert Mod 2GB
Sorry, but what you've suggested makes no sense to me. If your textboxes contain strings then all that your loop will actually do is set Typeval to be the value of the last control, or "0" if it is null. I'm not sure at all what you'd be trying to achieve here, and it is not what you asked in post #1.

The reason for typecasting to Long in my response was to ensure that Nz returns a value of 0, not the string value "0" which it may return depending on the context of how it is used. There is no need to use CStr if the textbox contains a string. Change Nz(..., 0)to Nz(...) as Nz(..., 0) is intended to return a number.

It is certainly possible to loop through a series of textboxes to access each value in turn, but what you've posted gives me no idea of why you would do so in your case.

-Stewart
Aug 17 '10 #4
Jerry Maiapu
259 100+
The question is: Why do you want to loop through every control on the form?

I suggest you tell the forum what you're trying to achieve rather than what you are trying to do.
There maybe a better way of handling your problem rather than looping through every record.

That way I think those who wish to help can have a clear background idea of what you're trying to achieve. Otherwise, any suggestions posted would contradict to what you were initially trying to bring across.
This would result in possible confusion that will delay what you're actually asking for.

Just guidelines there;
Aug 18 '10 #5
hannoudw
115 100+
I have a form that contain a lots of unbound text box. they are on a shape of a matrix (5*12)
and they are From, article ,size, quantity, price
from1,from2,.....,from12
size1,size2,.....size12
quantity1,quantity2,...quantity12
price1,price2,.....,price12
article1,article2,....article12

I want to be able to do this sql:
Expand|Select|Wrap|Line Numbers
  1. insert into InvoiceDetails value(1,123,17,1,1,15000,"R")
  2. insert into InvoiceDetails value(2,555,41,1,2,15000,"S")
  3. insert into InvoiceDetails value(3,456,35,1,1,15000,"R")
  4.  
InvoiceDetails (InvoiceDetailsNum as number
Aticle as number
size as number
InvoiceNum as number
Quantity as number
Price as number
From as String)

Here's the code that i wrote that Stewart Ross Inverness
helped me with, but it's not working it's giving me an error:
Can't find the field Article1 reffered to in your expression
Expand|Select|Wrap|Line Numbers
  1. Dim ArticleValue, SizeValue, QtyValue, PriceValue, DiffValue, AchatNumValue As Long
  2.     Dim FromValue As String
  3.  
  4.    Dim intCtrlNo As Integer
  5.    Const cCtrlArticle = "Article"
  6.    Const cCtrlSize = "size"
  7.    Const cCtrqty = "quantity"
  8.    Const cCtrlPrice = "price"
  9.    Const cCtrlDiff = "Difference"
  10.    Const cCtrlType = "Type"
  11.    Const cCtrlachnum = "achat_num"
  12.  
  13.    For intCtrlNo = 1 To 12
  14.    ArticleValue = CLng(Nz(Me.Controls(cCtrlArticle & intCtrlNo), 0)) 'this line is highlighted with yellow
  15.    SizeValue = CLng(Nz(Me.Controls(cCtrlSize & intCtrlNo), 0))
  16.    QtyValue = CLng(Nz(Me.Controls(cCtrqty & intCtrlNo), 0))
  17.    PriceValue = CLng(Nz(Me.Controls(cCtrlPrice & intCtrlNo), 0))
  18.    DiffValue = CLng(Nz(Me.Controls(cCtrlDiff & intCtrlNo), 0))
  19.    FromValue = CStr(Nz(Me.Controls(cCtrlType & intCtrlNo), "R"))
  20.    AchatNumValue = CLng(Nz(Me.Controls(cCtrlachnum & intCtrlNo), 0))
  21.  
  22.     If (Not IsNull(AchatNumValue)) Then
  23.     sql2 = "insert into achat values(" _
  24.             & CStr(newAchatNum) & "," _
  25.             & CStr(ArticleValue) & "," _
  26.             & CStr(SizeValue) & "," _
  27.             & CStr(newFactNum) & "," _
  28.             & CStr(QtyValue) & "," _
  29.             & CStr(PriceValue) & "," _
  30.             & CStr(DiffValue) & ",'" _
  31.             & CStr(FromValue) & "'); "
  32.     db.Execute (sql2)
  33.  
  34.      End Select
  35.     End If
  36.     Next intCtrlNo
  37.  
Aug 18 '10 #6
Stewart Ross
2,545 Expert Mod 2GB
I echo Jerry's comment - why are you doing it this way? There is no substitute for proper database design, and the un-normalised approach you are taking is bound to lead to complications which are avoidable if your tables are designed following relational principles from the start.

The method for looping through controls I provided is simple and works as I expected when I have tested it on on an unbound form.

As long as you are not testing your loop with the form concerned in design view - which will fail because you cannot access the value of a control when it is open in design view - I can only assume that you do not have a control of the name 'article1' on the form at the time you are running the routine.

I strongly advise that you rethink your approach, however.

-Stewart
Aug 18 '10 #7
hannoudw
115 100+
Ok Thanks Stewart
actually the code worked !
many thanks to you and to all the team :)
Aug 18 '10 #8

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

Similar topics

1
by: mikea_59 | last post by:
I would like to use XSLT to translate some tagged value text to XML elements like this: Input Doc: <data>x=1.234 y=ABC z="Hello World"</data> Output Doc: <x>1.234</value>
2
by: Tom Fitzgibbon | last post by:
Stupid question: How do I loop through many text boxes on a page and get values for each box and put into an array? For example document.CreateEvent.test1.value will not take an array value...
5
by: José Carlos | last post by:
Hi. I am trying send a text value when click in an image. If i write: onClick="javascript:document.client.submit();" send all form, but i want to send only a input type text (name = cod),...
7
by: Nick Calladine | last post by:
Hi On my form i have multiple select which all have an id value total1, total2, total3 etc so i am trying to detect how many there are and then use this to caculate a total. Is there a...
2
by: grahama2 | last post by:
Greetings. I am writing a database with a search feature to go through 1 table and extract relavant records to be displayed on a subform in spreadsheet style. I have 8 txt boxes that can all recieve...
8
by: SaltyBoat | last post by:
Needing to import and parse data from a large PDF file into an Access 2002 table: I start by converted the PDF file to a html file. Then I read this html text file, line by line, into a table...
13
by: kronecker | last post by:
I am trying to delete multiple lines in a text file using the following Private Sub Read_TextFile() Dim objReader As StreamReader Dim strfull, strContents, strContentsold, strContentsnew As...
14
by: cobolguy | last post by:
I am trying to add the cr/lf characters to a form text box. I have used both text and a memo field that is set for Rich Text. I can add them to a string, display the string in a message box - but...
2
by: Geethu03 | last post by:
Hi i got the array values from the html page using foreach. In this i can get multiple array values to print in the table. the values are Date Forward Process 12/9/8008 5 3...
6
by: iDesmet | last post by:
Good day! I was wondering if someone could show me the way with this little problem I have. I need to get the value/text of every checked SubItems in a listview so I can execute a Sub. The...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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...

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.