how to validate text file, loading into listbox

Please support our Visual Basic 4 / 5 / 6 advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved

Join Date: Sep 2006
Posts: 54
Reputation: royaloba is an unknown quantity at this point 
Solved Threads: 1
royaloba royaloba is offline Offline
Junior Poster in Training

Re: how to validate text file, loading into listbox

 
0
  #11
Oct 16th, 2006
sir yomet
hello sir i have some good news for you, i was able to set the TIMEOUT, but there is few error i want to fix..
SAMPLE OUTPUT mysql
--------------------------------------------------------------------------------
Date TimeIn Event CardNumber TimeOut
--------------------------------------------------------------------------------
2002-05-02 | 21:30:12 | Access | 451 | 21:32:09 |
2002-05-02 | 21:30:12 | Access | 451 | 21:32:09 |
2002-05-02 | 21:30:22 | Access | 451 | 21:32:09 |
2002-05-02 | 21:30:48 | Access | 546 | 21:32:09 |
2002-05-02 | 21:30:55 | Access | 546 | 21:32:09 |
2002-05-02 | 21:31:19 | Access | 546 | 21:32:09 |
2002-05-02 | 21:31:59 | Access | 234 | 21:32:09 |
2002-05-02 | 21:32:06 | Access | 234 | 21:32:09 |
2002-05-02 | 21:32:08 | Access | 234 | 21:32:09 |
2002-05-02 | 21:32:09 | Access | 234 | 21:32:09 |
---------------------------------------------------------------------------------
i just revise some of your sample code sir... as you see in the output i
the code read only the last TimeIn... the right output shoul be like this

--------------------------------------------------------------------------------
Date TimeIn Event CardNumber TimeOut
--------------------------------------------------------------------------------
2002-05-02 | 21:30:12 | Access | 451 | 21:32:22 |
2002-05-02 | 21:30:48 | Access | 546 | 21:31:19 |
2002-05-02 | 21:31:59 | Access | 234 | 21:32:09 |
--------------------------------------------------------------------------------
read only the first TimeIn and the LAST record and put this as TimeOut Base on their CARDNUMBER and the DATE...i need your suggestions sir...thanks..and if you can provide me a bit of code...thanks
THE CODE I USE SIR

Set conn = New ADODB.Connection
conn.Open "DRIVER={MySQL ODBC 3.51 Driver};" & "SERVER=localhost;" & " DATABASE=time_and_attendance;" & "UID=root;PWD=admin; OPTION=3"
While Not EOF(inFile)
Line Input #inFile, InLine
If InStr(InLine, "Event:Access") > 0 Then
data() = Split(InLine)
fields = UBound(data())

conn.Execute "INSERT INTO tblattendance (Date, TimeIn, Event, CardNumber) " & _
"VALUES ('" & data(0) & "', " & _
"'" & Left(data(1), 8) & "', " & _
"'" & Mid(data(2), 7) & "', " & _
"'" & Card(data(5)) & "')"

Set rs = New ADODB.Recordset
squery = "": squery = "Select * from tblattendance where Date='" & data(0) & "' and CardNumber='" & data(5) & "'"
Call ExecuteCommand
If rs.EOF Then ' DATA HAS BEEN USED
conn.Execute "UPDATE tblattendance set TimeOut = '" & Left(data(1), 8) & "'"
End If
rs.Close
Set rs = Nothing
End If
Reply With Quote Quick reply to this message  
Join Date: Nov 2005
Posts: 134
Reputation: Yomet is an unknown quantity at this point 
Solved Threads: 10
Yomet Yomet is offline Offline
Junior Poster

Re: how to validate text file, loading into listbox

 
0
  #12
Oct 18th, 2006
royaloba,

I can see where you code is going wrong and there are two quite simple errors.
1) What you do is adding a record every time you read a line wihtout first verifying if that card has already been used today.
2) The UPDATE statement later on will update all the records - not only the ones for that specific card and day. This error comes from my own code so I take full responsibility for it, I forgot to add the WHERE clause in the code I gave you.

Using your own code I will show you what I am talking about and how to correct it.
BTW, please use the code tags like this
[ code ]
Here goes all your code.
Please remove the spaces between the square brackets "[" and "]" and the "code" and "/code" parts
[ /code ]

OK, here goes
Visual Basic 4 / 5 / 6 Syntax (Toggle Plain Text)
  1. Set conn = New ADODB.Connection
  2. conn.Open "DRIVER={MySQL ODBC 3.51 Driver};" & "SERVER=localhost;" & " DATABASE=time_and_attendance;" & "UID=root;PWD=admin; OPTION=3"
  3. While Not EOF(inFile)
  4. Line Input #inFile, InLine
  5. If InStr(InLine, "Event:Access") > 0 Then
  6. data() = Split(InLine)
  7. fields = UBound(data())
  8.  
  9. '**********************
  10. 'At this point you have fresh data in your array and what you
  11. 'WANT to do is verify if the current card, in Card(data(5)), has
  12. 'already been used today.
  13.  
  14. 'What you need here is a condition to see if the card has already
  15. 'been used. Actually the IF statement you use later is almost there, i.e.
  16.  
  17. Set rs = New ADODB.Recordset
  18. squery = "": squery = "Select * from tblattendance where Date='" & data(0) & "' and CardNumber='" & data(5) & "'"
  19. Call ExecuteCommand
  20. 'If rs.EOF Then ' DATA HAS BEEN USED
  21. If NOT rs.EOF Then 'Data has NOT been used so insert it.
  22. '**********************
  23.  
  24. conn.Execute "INSERT INTO tblattendance (Date, TimeIn, Event, CardNumber) " & _
  25. "VALUES ('" & data(0) & "', " & _
  26. "'" & Left(data(1), 8) & "', " & _
  27. "'" & Mid(data(2), 7) & "', " & _
  28. "'" & Card(data(5)) & "')"
  29.  
  30. '**********************
  31. 'Now you have inserted the data into your database since it was
  32. 'not there to begin with.
  33.  
  34. 'However, if the data was already in the table you need to update
  35. 'the TimeOut with the current time. So we scrap the If statement
  36. '(since we used it above) and use the else clause.
  37.  
  38. Else
  39. '**********************
  40.  
  41. 'Set rs = New ADODB.Recordset
  42. 'squery = "": squery = "Select * from tblattendance where Date='" & 'data(0) & "' and CardNumber='" & data(5) & "'"
  43. 'Call ExecuteCommand
  44. 'If rs.EOF Then ' DATA HAS BEEN USED
  45.  
  46. 'conn.Execute "UPDATE tblattendance set TimeOut = '" & Left(data(1), 8) & "'"
  47. '**********************
  48. 'The new UPDATE statement
  49. conn.Execute "UPDATE tblattendance set TimeOut = '" & Left(data(1), 8) & "'" & _
  50. WHERE Date='" & 'data(0) & "' and CardNumber='" & data(5) & "'"
  51. '**********************
  52. End If
  53. rs.Close
  54. Set rs = Nothing
  55. End If

Seeing my last answer I would like to say sorry for misleading you, I was tired and didn't read the code correctly, there was no syntax error, just my eyes seeing double... :o

As far as I can see this should do the trick. Try it out and tell me how it works.

Happy coding

Yomet
Reply With Quote Quick reply to this message  
Join Date: Sep 2006
Posts: 54
Reputation: royaloba is an unknown quantity at this point 
Solved Threads: 1
royaloba royaloba is offline Offline
Junior Poster in Training

Re: how to validate text file, loading into listbox

 
0
  #13
Oct 19th, 2006
hello sir Yomet
first thanks for helping me with this project
the problem is when i put the "if not rs.eof then" the return record in the data base in empty... and when i replace it with "if rs.eof" the ouput for all the timeout is the same..sir i attach a sample program, i convert it to access so you could determine where is the problem occurs..thanks sir..
Attached Files
File Type: zip sample.zip (10.4 KB, 9 views)
Reply With Quote Quick reply to this message  
Join Date: Nov 2005
Posts: 134
Reputation: Yomet is an unknown quantity at this point 
Solved Threads: 10
Yomet Yomet is offline Offline
Junior Poster

Re: how to validate text file, loading into listbox

 
0
  #14
Oct 19th, 2006
royaloba,

Thanks for taking the time to convert your database into Access, it helped.

Now for what I found:
1) There seems to be some problem with the "ExecuteCommand" sub since it never gives back a recordset with records in it. However when I replaced that line with rs.Open squery, conn it gave the expected results.
2) I had to revert to your original code for inserting and updating records by using conn.Execute instead of db.Execute because when using the mix between DAO and ADODB the record was not saved by DAO before the ADODB went to find it. Weird I know but by using the following code everything went well.

Visual Basic 4 / 5 / 6 Syntax (Toggle Plain Text)
  1. Open sFile For Input As #inFile
  2. ' Set db = DBEngine.OpenDatabase(App.Path & "\db1.mdb")
  3. While Not EOF(inFile)
  4. Line Input #inFile, InLine
  5. If InStr(InLine, "Event:Access") > 0 Then
  6. data() = Split(InLine)
  7. fields = UBound(data())
  8.  
  9. Set rs = New ADODB.Recordset
  10. squery = "": squery = "Select * from tblattendance where Date=#" & data(0) & "# AND CardNumber='" & Card(data(5)) & "'"
  11. ' Call ExecuteCommand
  12. rs.Open squery, conn
  13. If rs.EOF Then
  14.  
  15. conn.Execute "INSERT INTO tblattendance ([Date], [TimeIn], CardNumber) " & _
  16. "VALUES (#" & data(0) & "#, " & _
  17. "'" & Left(data(1), 8) & "', " & _
  18. "'" & Card(data(5)) & "')"
  19.  
  20. 'Set rs = New ADODB.Recordset
  21. 'squery = "": squery = "Select * from tblattendance where Date='" & data(0) & "' and CardNumber='" & data(5) & "'"
  22. 'Call ExecuteCommand
  23. 'If rs.EOF Then ' DATA HAS BEEN USED
  24. Else
  25. conn.Execute "UPDATE tblattendance set TimeOut = '" & Left(data(1), 8) & "' where Date=#" & data(0) & "# AND CardNumber='" & Card(data(5)) & "'"
  26. End If
  27. rs.Close
  28. Set rs = Nothing
  29. End If
  30. Wend
  31. MsgBox "Record successfully saved ", vbInformation + vbOKOnly, "Record Saved"
  32. Unload Me
  33. Me.Show
  34. Close #inFile
  35. Exit Sub

You were correct in using If rs.EOF instead of If Not rs.EOF, typical brain-fart on my behalf.

Hope this code works and keep up the good work.

Yomet
Reply With Quote Quick reply to this message  
Join Date: Sep 2006
Posts: 54
Reputation: royaloba is an unknown quantity at this point 
Solved Threads: 1
royaloba royaloba is offline Offline
Junior Poster in Training

Re: how to validate text file, loading into listbox

 
0
  #15
Oct 20th, 2006
Sir Yomet,
thank you very much it's working very well i just replace the # because it's not valid in mysql... i can now move on to my project...
again THANKS
Reply With Quote Quick reply to this message  
Join Date: Nov 2005
Posts: 134
Reputation: Yomet is an unknown quantity at this point 
Solved Threads: 10
Yomet Yomet is offline Offline
Junior Poster

Re: how to validate text file, loading into listbox

 
0
  #16
Oct 20th, 2006
royaloba,

I am happy that things worked out well.

Good luck with the rest of the project

Happy coding

Yomet
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:



Other Threads in the Visual Basic 4 / 5 / 6 Forum


Views: 8384 | Replies: 15
Thread Tools Search this Thread



Tag cloud for Visual Basic 4 / 5 / 6
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC