Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

list box selection???

Status
Not open for further replies.

Muzzery

Programmer
Jan 11, 2002
72
GB
Hi

i need to take the data from a selected entry on a list box on one form to be put into fields on another open form.

Basically it is a search facillity. The user searches for something, all the results come up on screen, and the user chooses ONE of the results to view in full detail on another form.

I tried using the "For" command, but i can't remember how to use it!?!?!

any help appreciated,

Thanks

Muzz
 
Hi,

There are 2 forms of For...

For Each...Next and
For Each...Next Statement


Repeats a group of statements for each element in an array or collection.

Syntax

For Each element In group
[statements]
[Exit For]
[statements]

Next [element]

The For...Each...Next statement syntax has these parts:

Part Description
element Required. Variable used to iterate through the elements of the collection or array. For collections, element can only be a Variant variable, a generic object variable, or any specific object variable. For arrays, element can only be a Variant variable.
group Required. Name of an object collection or array (except an array of user-defined types).
statements Optional. One or more statements that are executed on each item in group.



Remarks

The For...Each block is entered if there is at least one element in group. Once the loop has been entered, all the statements in the loop are executed for the first element in group. If there are more elements in group, the statements in the loop continue to execute for each element. When there are no more elements in group, the loop is exited and execution continues with the statement following the Next statement.

Any number of Exit For statements may be placed anywhere in the loop as an alternative way to exit. Exit For is often used after evaluating some condition, for example If…Then, and transfers control to the statement immediately following Next.

You can nest For...Each...Next loops by placing one For...Each...Next loop within another. However, each loop element must be unique.

Note If you omit element in a Next statement, execution continues as if element is included. If a Next statement is encountered before its corresponding For statement, an error occurs.

You can't use the For...Each...Next statement with an array of user-defined types because a Variant can't contain a user-defined type.

For...Next
For...Next Statement


Repeats a group of statements a specified number of times.

Syntax

For counter = start To end [Step step]
[statements]
[Exit For]
[statements]

Next [counter]

The For…Next statement syntax has these parts:

Part Description
counter Required. Numeric variable used as a loop counter. The variable can't be a Boolean or an array element.
start Required. Initial value of counter.
end Required. Final value of counter.
step Optional. Amount counter is changed each time through the loop. If not specified, step defaults to one.
statements Optional. One or more statements between For and Next that are executed the specified number of times.



Remarks

The step argument can be either positive or negative. The value of the step argument determines loop processing as follows:

Value Loop executes if
Positive or 0 counter <= end
Negative counter >= end



After all statements in the loop have executed, step is added to counter. At this point, either the statements in the loop execute again (based on the same test that caused the loop to execute initially), or the loop is exited and execution continues with the statement following the Next statement.

Tip Changing the value of counter while inside a loop can make it more difficult to read and debug your code.

Any number of Exit For statements may be placed anywhere in the loop as an alternate way to exit. Exit For is often used after evaluating of some condition, for example If...Then, and transfers control to the statement immediately following Next.

You can nest For...Next loops by placing one For...Next loop within another. Give each loop a unique variable name as its counter. The following construction is correct:

For I = 1 To 10
For J = 1 To 10
For K = 1 To 10
...
Next K
Next J
Next I

Note If you omit counter in a Next statement, execution continues as if counter is included. If a Next statement is encountered before its corresponding For statement, an error occurs.

Take yer pic :) Skip,
metzgsk@voughtaircraft.com
 
Hello

Right now I'm doing a project in VB 6.
It is much concerning with list box.

What I want is after I click a command button, then one item will be added to a list box. BUT, I DO NOT want the same item written again in the list box.
So, it is basically like a search routine.

I tried my best, yet still...

Hope you could reply me.

Thank you very much.
 
Using VBA i'd try basing the List Box on a table, then add the item into the table, so that when the list box is next looked at, the item will be in there.

To make sure that a duplicate isn't added, i would use a DLookup command, and if the DLookup value is greater than 0, than i wouldn't allowthat item to be added to the table.

I'm afraid i'm not sure if this will work in VB, or even how to code it correctly, i haven't worked with it. But give it a go and see what hapens.

Muzz
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top