Click here to Skip to main content
15,885,936 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I am new to Visual Basic. I am using visual basic 6 with Microsoft Access database to view and record attendance data. I have been successful to connect the machine and view attendance data in DataGrid view using machine sdk. However in the database, there is only one row of data has imported. Need help. I have attached the code. Also I will be thankful if someone shows me how to improve it. Thanks

What I have tried:

VB
Private Sub cmdGlogData_Click()
   Dim vTMachineNumber As Long
   Dim vSMachineNumber As Long
   Dim vSEnrollNumber As Long
   Dim vVerifyMode As Long
   Dim vYear As Long
   Dim vMonth As Long
   Dim vDay As Long
   Dim vHour As Long
   Dim vMinute As Long
   Dim vErrorCode As Long
   Dim vRet As Boolean
   Dim i, n As Long
   Dim vMaxLogCnt  As Long    
   Dim vinouttype As Long
   Dim vaction As Long
    
   With RawData
      .ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & App.Path & "\db\dbase.mdb;Persist Security Info=False"
      .RecordSource = "select * from RawData"
      .Refresh         
        
      Do    
         vMaxLogCnt = gMaxLow    
         lblMessage.Caption = "Waiting..."   
         DoEvents       
         gridSLogData.Height = 4800
         gridSLogData.Redraw = False
         gridSLogData.Clear        
         gstrLogItem = Array("", "TMachineNo", "EnrollNo", "EMachineNo", "InOut", "VeriMode", "DateTime")
         With gridSLogData
            .Row = 0
            .ColWidth(0) = 600
            For i = 1 To 6
               .Col = i
               .Text = gstrLogItem(i)
               .ColAlignment(i) = 3
               .ColWidth(i) = 1200
            Next i
            .Col = 6
            .ColWidth(5) = 2000
            .ColWidth(6) = 2000
            .ColWidth(7) = 100
            .ColWidth(8) = 100
            n = .Rows
            If n > 2 Then
               Do
                  If n = 2 Then Exit Do
                  .RemoveItem (n)
                  n = n - 1
               Loop
            End If
            .Redraw = True
         End With   
         MousePointer = vbHourglass
         vRet = frm_DataImp.FP_CLOCK1.EnableDevice(mMachineNumber, False)
         If vRet = False Then
            lblMessage.Caption = gstrNoDevice
            MousePointer = vbDefault
            Exit Sub
         End If    
         vRet = frm_DataImp.FP_CLOCK1.ReadAllGLogData(mMachineNumber)
         If vRet = False Then
            frm_DataImp.FP_CLOCK1.GetLastError vErrorCode
            lblMessage.Caption = ErrorPrint(vErrorCode)
         End If    
         If vRet = True Then
            lblMessage.Caption = "Getting..."
            frm_DataImp.FP_CLOCK1.EnableDevice mMachineNumber, True
            MousePointer = vbHourglass
            DoEvents
            gridSLogData.Redraw = False        
            With gridSLogData
               i = 1
               Do
EEE:              vRet = frm_DataImp.FP_CLOCK1.GetAllGLogData(mMachineNumber, _
                                                              vTMachineNumber, _
                                                              vSEnrollNumber, _
                                                              vSMachineNumber, _
                                                              vVerifyMode, _
                                                              vYear, _
                                                              vMonth, _
                                                              vDay, _
                                                              vHour, _
                                                              vMinute)
                  If vRet = False Then Exit Do
                  If vRet = True And i <> 1 Then
                     .AddItem (1)
                  End If
                  vinouttype = Int(vVerifyMode / 8)
                  vaction = vVerifyMode Mod 8
                  .Row = i
                  .Col = 0
                  .Text = i
                  .Col = 1
                  .Text = vTMachineNumber
                  .Col = 2
                  .Text = vSEnrollNumber
                  .Col = 3
                  .Text = vSMachineNumber
                  .Col = 4
                  .Text = vinouttype
                  .Col = 5
                  If vSEnrollNumber <> 0 Then
                     If vaction = 0 Then
                        .Text = vaction & "/Normal"
                     ElseIf vaction = 1 Then
                        .Text = vaction & "/f1"
                     ElseIf vaction = 2 Then
                        .Text = vaction & "/f2"
                     ElseIf vaction = 3 Then
                        .Text = vaction & "/f3"
                     ElseIf vaction = 4 Then
                        .Text = vaction & "/f4"
                     ElseIf vaction = 5 Then
                        .Text = vaction & "/in"
                     ElseIf vaction = 6 Then
                        .Text = vaction & "/out"
                     Else
                        .Text = vaction & "/--"
                     End If
                  Else
                     If vaction = 0 Then
                        .Text = vaction & "/Closed"
                     ElseIf vaction = 1 Then
                        .Text = vaction & "/Opened"
                     ElseIf vaction = 2 Then
                        .Text = vaction & "/HandOpen"
                     ElseIf vaction = 3 Then
                        .Text = vaction & "/ProcOpen"
                     ElseIf vaction = 4 Then
                        .Text = vaction & "/ProcClose"
                     ElseIf vaction = 5 Then
                        .Text = vaction & "/IllegalOpen"
                     ElseIf vaction = 6 Then
                        .Text = vaction & "/IlleagalRemove"
                     ElseIf vaction = 7 Then
                        .Text = vaction & "/Alarm"
                     Else
                        .Text = vaction & "/--"
                     End If
                  End If
                  .Col = 6
                  .Text = CStr(vYear) & "/" & Format(vMonth, "0#") & "/" & Format(vDay, "0#") & _
                          " " & Format(vHour, "0#") & ":" & Format(vMinute, "0#")
                  DoEvents
                  i = i + 1
                  If i > vMaxLogCnt Then Exit Do
               Loop
            End With
        
            If i > vMaxLogCnt Then
               gridSLogData.Height = gridSLogData.Height / 2            
            End If
            vMaxLogCnt = vMaxLogCnt * 2
            If i > vMaxLogCnt Then
               gridSLogData.Height = gridSLogData.Height * 2 / 3
            End If
            gridSLogData.Redraw = True       
        
            lblMessage.Caption = "ReadAllGLogData OK"
         End If
    
         'Writing to database
         '
         '
         '
         With .Recordset
            .AddNew
            !TMachineNo = vTMachineNumber
            !EnrollNo = vSEnrollNumber
            !EMachineNo = vSMachineNumber
            !InOut = vinouttype
            If vSEnrollNumber <> 0 Then
               If vaction = 0 Then
                  !VeriMode = vaction & "/Normal"
               ElseIf vaction = 1 Then
                  !VeriMode = vaction & "/f1"
               ElseIf vaction = 2 Then
                  !VeriMode = vaction & "/f2"
               ElseIf vaction = 3 Then
                  !VeriMode = vaction & "/f3"
               ElseIf vaction = 4 Then
                  !VeriMode = vaction & "/f4"
               ElseIf vaction = 5 Then
                  !VeriMode = vaction & "/in"
               ElseIf vaction = 6 Then
                  !VeriMode = vaction & "/out"
               Else
                  !VeriMode = vaction & "/--"
               End If
            Else
               If vaction = 0 Then
                  !VeriMode = vaction & "/Closed"
               ElseIf vaction = 1 Then
                  !VeriMode = vaction & "/Opened"
               ElseIf vaction = 2 Then
                  !VeriMode = vaction & "/HandOpen"
               ElseIf vaction = 3 Then
                  !VeriMode = vaction & "/ProcOpen"
               ElseIf vaction = 4 Then
                  !VeriMode = vaction & "/ProcClose"
               ElseIf vaction = 5 Then
                  !VeriMode = vaction & "/IllegalOpen"
               ElseIf vaction = 6 Then
                  !VeriMode = vaction & "/IlleagalRemove"
               ElseIf vaction = 7 Then
                  !VeriMode = vaction & "/Alarm"
               Else
                  !VeriMode = vaction & "/--"
               End If
            End If
            !DateTime = CStr(vYear) & "/" & Format(vMonth, "0#") & "/" & Format(vDay, "0#") & _
                        " " & Format(vHour, "0#") & ":" & Format(vMinute, "0#")
            .Update
FFF:   
         End With
      Loop
      gGetState = False
    
      If .Recordset.RecordCount > 1 Then 
         .Recordset.MoveLast
   End With
   MousePointer = vbDefault       
    
   DoEvents
   frm_DataImp.FP_CLOCK1.EnableDevice mMachineNumber, True    
End Sub
Posted
Updated 3-Sep-17 8:22am
v4

You are going to find it difficult to get any help with VB6 code as very few, if any, still use it.

Some feedback... You have too much code in the cmdGlogData_Click event. Break it up into single responsibility chunks. This will help greatly with reading your code... Something like...
VB
Private Sub cmdGlogData_Click()

    InitializeGrid(gridSLogData)

    If InitializeDeviced() Then
        GetDeviceDataAndSave(gridSLogData)
    Else
        lblMessage.Caption = gstrNoDevice
        MousePointer = vbDefault
    End If

end Sub

Private Sub InitializeGrid(gridLog as GridView)
    ' config grid here
End Sub

Private Function InitializeDevice(mMachineNumber as long) as Boolean

    dim success as Boolean

    success = frm_DataImp.FP_CLOCK1.EnableDevice(mMachineNumber, False)

    return success

End Sub

Private Sub GetDeviceDataAndSave(mMachineNumber as long, gridLog as GridView)

    Dim vTMachineNumber As Long
    Dim vSMachineNumber As Long
    Dim vSEnrollNumber As Long
    Dim vVerifyMode As Long
    Dim vYear As Long
    Dim vMonth As Long
    Dim vDay As Long
    Dim vHour As Long
    Dim vMinute As Long

    ' get data entry from device
    If frm_DataImp.FP_CLOCK1.GetAllGLogData(mMachineNumber, _
                                            vTMachineNumber, _
                                            vSEnrollNumber, _
                                            vSMachineNumber, _
                                            vVerifyMode, _
                                            vYear, vMonth, vDay, _
                                            vHour, vMinute) Then
        ' success
        If SaveDataEntry(mMachineNumber, _
                         vTMachineNumber, _
                         vSEnrollNumber, _
                         vSMachineNumber, _
                         vVerifyMode, _
                         vYear, vMonth, vDay, _
                         vHour, vMinute) Then

            LogEntry(gridLog, mMachineNumber, _
                              vTMachineNumber, _
                              vSEnrollNumber, _
                              vSMachineNumber, _
                              vVerifyMode, _
                              vYear, vMonth, vDay, _
                              vHour, vMinute)
        Else
            ' failed to save to database
        End If
    Else
        ' failed to read device code here
    End If

End Sub

Private Function SaveDataEntry(vTMachineNumber As Long, vSMachineNumber As Long _
                               vSEnrollNumber As Long, vVerifyMode As Long, _
                               vYear As Long, vMonth As Long, vDay As Long, _
                               vHour As Long, vMinute As Long) as Boolean

    dim success as Boolean

    ' Write to DB here and set success to true or false

    return success

End Function

NOTE: This is only a mock-up of how you could possibly structure the code...

Now you can see each part of the process and monitor each write to the database as it is encapsulated in its own function. Debugging your code has now become a lot easier...
 
Share this answer
 
Comments
Urusaki 3-Sep-17 10:42am    
Thank you so much. It is a great help. Any feedback on how to write multiple or all rows to the database?
Graeme_Grant 3-Sep-17 10:48am    
I have not done VB6 for approx 15 years, so I am not familiar with it anymore. VB.NET or C# is a very different story.

However, when working with databases, bulk operations are usually wrapped in Begin/Commit/RollBack Transactions.
Urusaki 3-Sep-17 11:32am    
Well, it's ok. By the way, What is the syntax on VB.Net for this purpose?
Graeme_Grant 3-Sep-17 11:35am    
VB.Net? It's a bit more than that.
Wendelius 3-Sep-17 14:01pm    
Good advice, a 5.
I honestly believe that the best advice for you is to drop Visual Basic 6. The last version was published 1998 and Microsoft stopped supporting it at 2008. So there is no guarantee that VB6 environment will continue to work in future OS versions.

If visual basic is your preference then consider using Visual Basic .NET

More information:
- Visual Basic - Wikipedia[^]
- Visual Basic .NET[^]
 
Share this answer
 
Comments
Graeme_Grant 3-Sep-17 10:37am    
5'd
Wendelius 3-Sep-17 14:00pm    
It's been quite awhile since I've last used VB&, but looking at the code, you have multiple exits from the loop and from the sub. Have you debugged the execution and verified that the loop is really executed several times?

For example if gMaxLow is 1 then the loop is exited after first round....
I would suggest you stop using VB6. It teaches you bad habits that you're going to have to unlearn later on.

You can get the free version of Visual Studio here[^], which will include VB.NET, C#, C/C++, whatever, and allow you to write just about any type of application you want.

Just looking at the code you've posted, you've already got a lot to unlearn. I would suggest picking a book on VB.NET and working through that instead.
 
Share this answer
 
Comments
Urusaki 3-Sep-17 10:24am    
Well, I'll keep is on mind and will upgrade to VB.Net soon. Yet, How to get all the rows of data? Please shed some light on this matter.
Dave Kreskowiak 3-Sep-17 10:47am    
I couldn't tell you. The problem is that where you go an get a record using the SDK, you're getting a single row, which I assume is being put into the variables you're supplying to the call:
GetAllGLogData(mMachineNumber, _
                                                 vTMachineNumber, _
                                                 vSEnrollNumber, _
                                                 vSMachineNumber, _
                                                 vVerifyMode, _
                                                 vYear, _
                                                 vMonth, _
                                                 vDay, _
                                                 vHour, _
                                                 vMinute)

I assume it returns a boolean to tell you that the recrod was retrieved successfully.

By the way, you do NOT have to check for False in one If statement and then check for True in another If statement. If the value is True, the If statement will execute the code in the Then block. If the value is False, the If statement will execute the code in the Else block:
    If expression Then
        ... code if true ...
    Else
        ... code if false ...
    End If


The problem is with the SDK. How do you tell it to move to the next record in the data? We don't know. We know nothing about the SDK you're using. That kind of support would have to come from the vendor of the SDK, not some third party that has never heard of it.
Urusaki 3-Sep-17 10:53am    
Actually sdk is working fine with the DataGridView (gridSLogData). All the stored data is being displayed on the gridSLogData. I think there is something that I do not know is missing on the Code of the database import, that is why there is only one row is coming.
Dave Kreskowiak 3-Sep-17 11:02am    
I don't know because I have no idea how the SDK is returning the data. All of the variables you're passing into the GetAllGLogData method can only hold a single value and you're only calling that method once as far as I can tell, so you're only getting a single record from the SDK.

Again, how do you tell the SDK to move to the next record? I don't know because I've never used that SDK, nor do I ever plan to.

What does the documentation on the SDK say about it?

Urusaki 3-Sep-17 11:10am    
The Datagrid has all the data, Dave. The code for the gridSLogData above did it. I just want that datas to be stored on the database.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900