• 0

VB Need some HELP! ASAP


Question

Can some one PLEASEEEE HELPPPP MEEEEE?????

I need some help with Arrays and Loops. (Visual Basic) I need to create a program that will store up to 5 numbers entered by the user into the Array, and then retrieve it.

1) The user has to enter one number at the time into the Text Box called (txtData.Text)

2) When the user will click on the ?Show Values? button, the numbers should then be displayed inside a Message Box.

Small screenshot:

vb4.jpg

(if the image won't load click HERE! )

Here is the code:

'Declaring Variables

Dim strNumber As String

Dim intArray(1 To 5) As Integer

Dim i As Integer

Dim j As Integer

Dim intIndex As Integer

Private Sub cmdNumber_Click()

'Click here to set the number.

Select Case cboNumber

Case ""

MsgBox "You must select the number of Entries First!", vbOKOnly, "Error Message!"

Case 1

strNumber = 1

Case 2

strNumber = 2

Case 3

strNumber = 3

Case 4

strNumber = 4

Case 5

strNumber = 5

End Select

End Sub

Private Sub cmdOk_Click()

'Arrays with Numbers

intIndex = 0

If txtData.Text = "" Or strNumber = "" Then

MsgBox "You have either not set the allowed number of entries or the array is full!", vbOKOnly, "Error Message!"

Else

For i = 1 To strNumber

? Here is the code to store the data into the array but it doesn?:no:rk :no:

intIndex = (intIndex + 1)

intArray(intIndex) = Val(txtData.Text)

txtData.Text = ""

Next i

j = j + 1

End If

End Sub

Private Sub mnuAllowed_Click()

'Displays an Message Box This Code Works

If strNumber = "" Then

MsgBox "0", vbOKOnly, "Entries Allowed:"

Else

MsgBox strNumber, vbOKOnly, "Entries Allowed:"

End If

End Sub

Private Sub mnuMade_Click()

'Displays the number of user entries made This Code Works

If j = "0" Then

MsgBox "0", vbOKOnly, "Entries Made:"

Else

MsgBox j, vbOKOnly, "Entries Made:"

End If

End Sub

Private Sub mnuValues_Click()

'Displays user entries in a Message Box

intIndex = 1

For i = 1 To j

? Here is the code to show the numbers entered by the user inside a Message Box, but it?s n:no:orking :no:

MsgBox intArray(i), vbOKOnly, "User Input:"

Next i

End Sub

Edited by k2storm
Link to comment
Share on other sites

25 answers to this question

Recommended Posts

  • 0

have you tried with collection?

[Edit]

What do you mean with "but it's not working".

Which is the error?

Edited by _mastermind_
Link to comment
Share on other sites

  • 0

i found the error

Before:

For i = 1 To strNumber

intIndex = (intIndex + 1)

intArray(intIndex) = Val(txtData.Text)

txtData.Text = ""

Next i

j = j + 1

NOW

For i = 1 To strNumber

intIndex = (intIndex + 1)

intArray(intIndex) = Val(txtData.Text)

txtData.Text = ""

j = j + 1

Next i

So when you do the last loop to dispaly the msgboxes, J will be not 0.

[Edit]

Reading the second time the code, i see a strange thing:

Dim strNumber As String (Why you declared it as string, when you use it as int?)

Link to comment
Share on other sites

  • 0

I noticed a couple of things you could do better.

Instead of doing a huge case statement to set a number, you can just grab that number and set strNumber directly to it.

On cmdOK why do you have a variable that you increment on each loop when you could either

a) use 'i', intArray(i) = Val(txtData.Text) or

b) For intIndex = 1 To strNumber

Neither of these will make it work though. :p What error do you get?

Link to comment
Share on other sites

  • 0

It?s Visual Basic 6!

Here is the exe file: Lab4MM or click here: http://www.freewebs.com/k2gtstorm/Lab4MM.exe

When you run the program and start to add numbers inside (txtData.Text) Text Box, the application then should store the numbers inside the array, and when you click on the ?Show Values? button it should then display all the numbers in the same order the user has entered them.

It still doesn't work...... For some reason when you click on the "Show Values" button, it then displays the last number the user has entered and then 0.

For example: I entered two numbers: 1 and 2, (1 then I press OK, 2 and I press OK), when I click on the Show Values button it then displays the last number I have entered and that will be 2 and then it displays 0 but it should be 1 .........

Edited by k2storm
Link to comment
Share on other sites

  • 0

I think that this code is a too much complicated for the thing that sould do.

I think you should delete your code and rewrite it, if you not find the error.

Btw, my solution should be:

Dim arraylist as new Collection
Dim i as integer

Private From_Load()
 ? i=0
End sub

Private Sub cmdOk_Click()
 ? arraylist.Add txtData.text
 ? i=i+1
End Sub

Private Sub mnuValues_Click()
 ? int j
 ? string str
 ? For j=0 to i
 ? ? ? ? str=str ?& " " & arraylist.item(j)
 ? next
 ? MsgBox str
End sub

I don't know if the syntax is correct because i use vb.net and c# from about 4 years.

Link to comment
Share on other sites

  • 0
NOW

For i = 1 To strNumber

?  intIndex = (intIndex + 1)

?  intArray(intIndex) = Val(txtData.Text)

?  txtData.Text = ""

?  j = j + 1

Next i

This doesn?t solve the problem, when I run the program j is then multiplied byFor example:e: When I enter two numbers then (j = 4), and it should be (j = 2)

For i = 1 To strNumber

  intIndex = (intIndex + 1)

  intArray(intIndex) = Val(txtData.Text)

  txtData.Text = ""

Next i

j = j + 1

When I leave the code as it is then j has the correct value.

As for the Dim strNumber As String when I use it as int, I get an error me:blush:blush:

Any other suggestions???

Edited by k2storm
Link to comment
Share on other sites

  • 0

During the waiting for your reply, i installed VB6 and did this:

Dim arraylist As New Collection

Private Sub Command1_Click()
arraylist.Add (Text1.Text)
End Sub

Private Sub test_Click()
Dim i As Integer
For i = 1 To arraylist.Count
    MsgBox arraylist.Item(i)
Next
End Sub

And it works.

Link to comment
Share on other sites

  • 0

I got it ......... Thanks alot you saved my life!

Is this homework?

This assignment was driving me crazy for 2 weeks, It's not homework, it's an exercise I was working on ......

Link to comment
Share on other sites

  • 0
here is the file, i'm watching your file.

and i continue to have problem, to understand what do the form :D :D :D

loool weenur

Yes, so did I :D

Thank you once again for your help! :) :D

Link to comment
Share on other sites

  • 0
Yeah, it's so funny helping someone cheat. :rolleyes: No wonder so much software sucks these days. The developers can't even get through 101 on their own.

Yeah, I?m just learning, next semester I?m planning on taking Visual Basic class ?.. and I?m trying to get some practice before I take the class. I?m still in school, and so far I have only completed HTML, JavaScript, Programming C class and that all.

And it's not cheating if some one asks for help. I was working on this program for some time now, and each time I was working on this problem I was hitting brick wall. So that?s was why I came here to ask for help???

Edited by k2storm
Link to comment
Share on other sites

  • 0

Asking for help is cool. Someone pointing out what you need to fix, or hinting at what you need to do is cool. Doing it for you isn't cool.

Some of us are a little anal about the whole cheating thing.

Anyhoo... if it isn't homework, I have no complaint. :) Good on ya for taking some initiative.

Link to comment
Share on other sites

  • 0

Yeah, i don't like people like that

I knew this guy, who couldn't code to save his life, but he wanted to be a programmer, i was helping him on one of his assignments (more like doing a it of it) i fixed the problem he was having. Anyway, a few day's later he said he needed my help again, i looked at his code and it was completely different, i asked him about it and he said he just changed it, i fixed it again and asked one of our friends, apparently the guy was deleting all the source (vb6 day's so not the form layout) and copying new code over he got off the net, he hooked the event's up. cause he never took the initiative to write his own code, he failed the subject.

Ever since then i have had a problem with helping out on big hunks of code, but since this is only a exercise, i don't mind

Edit: _mastermind_, i didn't know vb6 had a collection object, thanks for showing me the error's of my ways.

Link to comment
Share on other sites

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.