|
|
|
write/read file
|
Original Message
|
Name: Luzo
Date: June 7, 2003 at 14:38:25 Pacific
Subject: write/read fileOS: XP homeCPU/Ram: 1.2Ghz 504mb ram |
Comment: this program stores the ID number, name, book title, date, date due. This is a library program. CAN U CODE THIS IN VB i want to write the ID number(txtID), the name(txtName), the book(txtTitle), the date(txtDate) and the date due(txtDue). then i want to read files. when i type the name the info(ID, name,title etc)to be displayed in list boxes (5 of them, one each). I want to read/load more than one file at a time. The list boxes are called List1,List2,List3,List4,List5. can u help?
Report Offensive Message For Removal
|
|
Response Number 1
|
Name: Chi Happens
Date: June 13, 2003 at 19:53:38 Pacific
Subject: write/read file |
Reply: (edit)You can use a flat file or an access database or better yet SQL. If you use a flat file, you will use something like: Private Sub LoadFlatFile(ByVal FileSpec As String) Dim DataLine As String Dim FP As Long Dim Pos As Long Dim DataField As Single List1.Clear List2.Clear List3.Clear List4.Clear List5.Clear FP = FreeFile() Open FileSpec For Input As #FP Do While Not EOF(FP) DataField = 0 Line Input #FP, DataLine Do While Len(DataLine) > 0 Pos = InStr(1, DataLine, ",") If Pos > 0 Then Select Case DataField Case 0: List1.AddItem Left(DataLine, Pos - 1) Case 1: List2.AddItem Left(DataLine, Pos - 1) Case 2: List3.AddItem Left(DataLine, Pos - 1) Case 3: List4.AddItem Left(DataLine, Pos - 1) End Select DataLine = Mid(DataLine, Pos + 1, Len(DataLine)) DataField = DataField + 1 Else List5.AddItem DataLine DataLine = "" End If Loop Loop Close #FP End Sub Private Sub SaveFlatFile(ByVal FileSpec As String) Dim DataLine As String Dim FP As Long Dim ItemIndex As Long DataLine = "" For ItemIndex = 0 To List1.ListCount - 1 DataLine = DataLine & List1.List(ItemIndex) & "," & List2.List(ItemIndex) & "," & List3.List(ItemIndex) & "," & List4.List(ItemIndex) & "," & List5.List(ItemIndex) & vbCrLf Next ItemIndex FP = FreeFile() Open FileSpec For Append As #FP Print #FP, DataLine Close #FP End Sub For access or sql you would do something like: Private Sub SaveToDatabase(ByVal ConnectionString As String) Dim CN As ADODB.Connection Dim SQL As String Dim Field1 As String Dim Field2 As String Dim Field3 As String Dim Field4 As String Dim Field5 As String Dim ItemIndex As Long Set CN = CreateObject("ADODB.Connection") CN.Open ConnectionString For ItemIndex = 0 To List1.ListCount - 1 Field1 = Replace(List5.List(ItemIndex), "'", "''") Field2 = Replace(List5.List(ItemIndex), "'", "''") Field3 = Replace(List5.List(ItemIndex), "'", "''") Field4 = Replace(List5.List(ItemIndex), "'", "''") Field5 = Replace(List5.List(ItemIndex), "'", "''") SQL = "INSERT INTO YourTableName (IDnumber, thename, thebook, thedate, thedatedue) VALUES('" & Field1 & "','" & Field2 & "','" & Field3 & "','" & Field4 & "','" & Field5 & "')" CN.Execute SQL Next ItemIndex CN.Close Set CN = Nothing End Sub Private Sub LoadFromDatabase(ByVal ConnectionString As String, Optional ByVal IDNumber As String = "", Optional ByVal TheName As String = "", Optional ByVal TheBook As String = "", Optional ByVal SortField As String = "") Dim CN As ADODB.Connection Dim RS As ADODB.Recordset Dim SQL As String Dim ItemIndex As Long Dim Fields(3) As Boolean List1.Clear List2.Clear List3.Clear List4.Clear List5.Clear Fields(0) = False Fields(1) = False Fields(2) = False Set CN = CreateObject("ADODB.Connection") Set RS = CreateObject("ADODB.RecordSet") CN.Open ConnectionString SQL = "SELECT * FROM YourTableName WHERE (" If IDNumber "" Then SQL = SQL & "IDNumber LIKE '%" & IDNumber & "%'" Fields(0) = True End If If TheName "" Then If Fields(0) Then SQL = SQL & "AND thename LIKE '%" & TheName & "%'" Else SQL = SQL & "thename LIKE '%" & TheName & "%'" End If Fields(1) = True End If If TheName "" Then If Fields(0) Or Fields(1) Then SQL = SQL & "AND thebook LIKE '%" & TheName & "%'" Else SQL = SQL & "thebook LIKE '%" & TheName & "%'" End If Fields(2) = True End If If Fields(0) Or Fields(1) Or Fields(2) Then If SortField "" Then SQL = SQL & ") ORDER BY " & SortField Else SQL = SQL & ")" End If Else If SortField "" Then SQL = "SELECT * FROM YourTableName ORDER BY " & SortField Else SQL = "SELECT * FROM YourTableName" End If End If RS.Open SQL, CN Do While RS.EOF = False List1.AddItem CStr(RS("IDNUmber").Value & "") List2.AddItem CStr(RS("thename").Value & "") List3.AddItem CStr(RS("thebook").Value & "") List4.AddItem CStr(RS("thedate").Value & "") List5.AddItem CStr(RS("thedatedue").Value & "") Loop RS.Close CN.Close Set RS = Nothing Set CN = Nothing End Sub Of course you should trap errors and such, but the code above should function correctly. Hope this helps, Chi Happens
Report Offensive Follow Up For Removal
|
|
Response Number 2
|
Name: Chi Happens
Date: June 13, 2003 at 19:57:46 Pacific
Subject: write/read file |
Reply: (edit)Oops, I am glad i tried to see what the post looks like because there is a code error: In the SaveToDatabase sub it reads: Field1 = Replace(List5.List(ItemIndex), "'", "''") Field2 = Replace(List5.List(ItemIndex), "'", "''") Field3 = Replace(List5.List(ItemIndex), "'", "''") Field4 = Replace(List5.List(ItemIndex), "'", "''") Field5 = Replace(List5.List(ItemIndex), "'", "''") But it should read: Field1 = Replace(List1.List(ItemIndex), "'", "''") Field2 = Replace(List2.List(ItemIndex), "'", "''") Field3 = Replace(List3.List(ItemIndex), "'", "''") Field4 = Replace(List4.List(ItemIndex), "'", "''") Field5 = Replace(List5.List(ItemIndex), "'", "''") I didn't test the code, or i would have found it before i posted. THIS IS AN EXAMPLE OF WHAT HAPPENS WHEN YOU CUT AND PASTE LOL :)
Report Offensive Follow Up For Removal
|
|
Response Number 3
|
Name: Chi Happens
Date: June 13, 2003 at 20:00:42 Pacific
Subject: write/read file |
Reply: (edit)Darn it... i found another copy and paste issue... the second If TheName "" Then If Fields(0) Or Fields(1) Then SQL = SQL & "AND thebook LIKE '%" & TheName & "%'" Else SQL = SQL & "thebook LIKE '%" & TheName & "%'" End If Fields(2) = True End If should be If TheBook "" Then If Fields(0) Or Fields(1) Then SQL = SQL & "AND thebook LIKE '%" & TheBook & "%'" Else SQL = SQL & "thebook LIKE '%" & TheBook & "%'" End If Fields(2) = True End If phew...i guess i need some sleep.
Have fun, Chi Happens
Report Offensive Follow Up For Removal
|
Use following form to reply to current message:
|
|

|