|
|||||||
![]() |
|
|
LinkBack | Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
|
#1 |
|
Member (9 bit)
Join Date: Jan 2002
Location: Quebec, Canada
Posts: 426
|
how do I retreice data from a file?!
Hi,
I've make a little program that stores the name and phone numbers. I've did a database (base.txt) and looks like this: 1µBurgerµ634-4345 2µDionµ434-4345 3µFonfonµ334-3422 The number, then when the program find the sign µ, it displays what it is after, then when it finds the other µ, it displays the phone number (the first number and the name are displayed in a combobox, the phone number in a textbox) then the part to retreive data that doesn't work! (my friend did that I didn't had an idea how to do it) Private Sub Form_Load() e = 1 Open "base.txt" For Input As #1 Do While Not EOF(1) Line Input #1, a(e) b = 0: c = 0: d = 0 For b = 1 To Len(a(e)) f = b If Mid$(a(e), b, 1) = "µ" Then If c = 1 Then d = f: Exit For ElseIf c = 0 Then c = 1 End If Next b nom1.AddItem Left$(a(e), 1) & ". " & Mid$(a(e), 3, d + 3) e = e + 1 Loop End Sub (nom1 = combobox) if anyone had another way to do it, please any help would be appreciated !!!! Thanks! Dim3x |
|
|
|
|
|
#2 |
|
Professional gadfly
|
Instead of using the Mid$ function, use the InStr (In String) function. Because you know that each line will have two µs in there, that function would be far less messy than what you are trying to do.
|
|
|
|
|
|
#3 |
|
Member (10 bit)
|
InStr() will return the location of the string you're searching for. You'll still need to use Mid$ to actually retrieve the data.
|
|
|
|
|
|
#4 | |
|
Professional gadfly
|
Quote:
Public Sub Form_Load() Dim e As Integer, a As Integer, b As Integer, sa(255) As String Open "base.txt" For Input As #1 e = 0 Do While Not EOF(1) Line Input #1, sa(e) a = InStr(sa(e), "µ") 'Find first µ b = InStr(a + 1, sa(e), "µ") 'Find second µ nom1.AddItem Left$(sa(e), a - 1) & ". " & Mid$(sa(e), a + 1, b - a - 1) & ": " & Mid$(sa(e), b + 1) e = e + 1 Loop Close #1 End Sub |
|
|
|
|
|
|
#5 |
|
Member (10 bit)
|
Nice use of descriptive variables
Also, be sure not to have more than 255 lines in your file or you'll get an error. |
|
|
|
|
|
#6 | |
|
Professional gadfly
|
Quote:
![]() If I were doing this for real, I would have use real variable names, and probably sized the array to the file length. But this works for three entries. |
|
|
|
|
|
|
#7 |
|
Member (9 bit)
Join Date: Jan 2002
Location: Quebec, Canada
Posts: 426
|
ok now it retreives the number and the name. but when I click on the name it doesn't show the phone number in the textbox (numero1)
Public Sub Form_Load() Open "base.txt" For Input As #1 e = 0 Do While Not EOF(1) Line Input #1, sa(e) a = InStr(sa(e), "µ") 'Find first µ b = InStr(a + 1, sa(e), "µ") 'Find second µ nom1.AddItem Left$(sa(e), a - 1) & ". " & Mid$(sa(e), a + 1, b - a - 1) e = e + 1 Loop Close #1 End Sub that's the part the program is loading the data. and I've put something if the user change the name in the combobox, it change it to the default thing. but if he puts a number, the program crashes because of the select case... Private Sub nom1_Change() Select Case Left$(nom1.Text, 1) Case 1, 2, 3, 4, 5, 6, 7, 8, 9 a = InStr(nom1.Text, ".") c = Left$(nom1.Text, a - 1) f = InStr(sa(c), "µ") f = InStr(f + 1, sa(c), "µ") numero1.Text = Mid$(sa(c), f + 1) Case Else nom1.Text = "Choisissez un nom" End Select End Sub this is also supposed to change the textbox when the user selects a name but it doesn't. Thanks Dim3x |
|
|
|
|
|
#8 |
|
Member (10 bit)
|
I'm a little confused about you're problems, but I think I get what you're saying.
1.) Why don't you change the Style of the combo box to 'Dropdown List' that way the user can't change the values in the combo box. 2.) You're using the change() event, switch that to the click() event. The change() event is if the values in the combo box changes. Hope this is what you were asking. |
|
|
|
|
|
#9 |
|
Member (9 bit)
Join Date: Jan 2002
Location: Quebec, Canada
Posts: 426
|
ok I'll try that thanks
|
|
|
|
![]() |
| Bookmarks |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|