Results 1 to 15 of 15

Thread: [RESOLVED] Datagrid Help

  1. #1

    Thread Starter
    Frenzied Member
    Join Date
    Jul 2004
    Posts
    1,202

    Resolved [RESOLVED] Datagrid Help

    Can anyone help me please.

    I want it so when i click on the grid that record shows in the text boxs can someone help using the code below.

    VB Code:
    1. Public CN As New ADODB.Connection
    2. Public RS As New ADODB.Recordset
    3.  
    4. Private Sub cmdClose_Click()
    5. CN.Close
    6. Unload Me
    7. End Sub
    8.  
    9. Private Sub cmdDelete_Click()
    10.     With RS
    11.         If MsgBox("Are You Sure?", vbCritical + vbYesNo, "Confirm") = vbYes Then
    12.  
    13.         CN.Execute "DELETE * FROM Subjects WHERE Subject ='" & .Fields("Subject") & "'"
    14.         MsgBox "Deleted", vbInformation, "Deleted"
    15.         .Requery
    16.         End If
    17.     End With
    18. End Sub
    19.  
    20. Private Sub cmdFirst_Click()
    21. On Error Resume Next
    22. With RS
    23.     .MoveFirst
    24. If .EOF = True Then
    25.     .MoveLast
    26. End If
    27.             txtSubject.Text = .Fields("Subject")
    28.             txtNoOfBooks.Text = .Fields("NoOfBooks")
    29.             txtDays.Text = .Fields("IssueDays")
    30.             txtFine.Text = .Fields("Finecharge")
    31.             txtReverse.Text = .Fields("ReserveCharge")
    32. End With
    33.  
    34. End Sub
    35.  
    36. Private Sub cmdLast_Click()
    37. On Error Resume Next
    38. With RS
    39.     .MoveLast
    40. If .EOF = True Then
    41.     .MoveFirst
    42. End If
    43.             txtSubject.Text = .Fields("Subject")
    44.             txtNoOfBooks.Text = .Fields("NoOfBooks")
    45.             txtDays.Text = .Fields("IssueDays")
    46.             txtFine.Text = .Fields("Finecharge")
    47.             txtReverse.Text = .Fields("ReserveCharge")
    48. End With
    49. End Sub
    50.  
    51. Private Sub cmdNew_Click()
    52.     cmdSave.Enabled = True
    53.     txtSubject.Text = ""
    54.     txtNoOfBooks.Text = ""
    55.     txtDays.Text = ""
    56.     txtFine.Text = ""
    57.     txtReverse.Text = ""
    58.     RS.AddNew
    59.  
    60. End Sub
    61.  
    62. Private Sub cmdNext_Click()
    63. On Error Resume Next
    64. With RS
    65.     .MoveNext
    66. If .EOF = True Then
    67.     .MoveFirst
    68. End If
    69.             txtSubject.Text = .Fields("Subject")
    70.             txtNoOfBooks.Text = .Fields("NoOfBooks")
    71.             txtDays.Text = .Fields("IssueDays")
    72.             txtFine.Text = .Fields("Finecharge")
    73.             txtReverse.Text = .Fields("ReserveCharge")
    74. End With
    75.  
    76. End Sub
    77.  
    78. Private Sub cmdPrevious_Click()
    79. On Error Resume Next
    80. With RS
    81.     .MovePrevious
    82. If .EOF = True Then
    83.     .MoveLast
    84. End If
    85.             txtSubject.Text = .Fields("Subject")
    86.             txtNoOfBooks.Text = .Fields("NoOfBooks")
    87.             txtDays.Text = .Fields("IssueDays")
    88.             txtFine.Text = .Fields("Finecharge")
    89.             txtReverse.Text = .Fields("ReserveCharge")
    90. End With
    91.  
    92. End Sub
    93.  
    94. Private Sub cmdSave_Click()
    95.  With RS
    96.          If MsgBox("Are You Sure?", vbQuestion + vbYesNo, "Confirm") = vbYes Then
    97.         If RS.EOF Then
    98.             .AddNew
    99.         End If
    100.         If txtSubject.Text <> "" And txtNoOfBooks.Text <> "" And txtDays.Text <> "" And txtFine.Text <> "" And txtReverse.Text <> "" Then
    101.         .Fields("Subject") = txtSubject.Text
    102.         .Fields("NoOfBooks") = txtNoOfBooks.Text
    103.         .Fields("IssueDays") = txtDays.Text
    104.         .Fields("FineCharge") = txtFine.Text
    105.         .Fields("ReserveCharge") = txtReverse.Text
    106.         .Update
    107.         MsgBox "Saved", vbInformation, "Saved"
    108.     End If
    109.    
    110. End If
    111. End With
    112. End Sub
    113.  
    114. Private Sub cmdSearch_Click()
    115. With RS
    116.     .Close
    117.     RS.Open "Select * FROM Subjects WHERE subject ='" & txtSearch.Text & "'"
    118.     txtSearch.Text = .Fields("Subject")
    119.    
    120.     .Requery
    121. End With
    122.  
    123. End Sub
    124.  
    125. Private Sub Command1_Click()
    126. On Error Resume Next
    127. With RS
    128.     If MsgBox("Update The Record?", vbQuestion + vbYesNo, "Confirm") = vbYes Then
    129.     .Fields("Subject") = txtSubject.Text
    130.     .Fields("NoOfbooks") = txtNoOfBooks.Text
    131.     .Fields("IssueDays") = txtDays.Text
    132.     .Fields("FineCharge") = txtFine.Text
    133.     .Fields("ReverseCharge") = txtReverse.Text
    134.     .Update
    135.     .Requery
    136. MsgBox "The current record has been updated", vbInformation, "Update Successful"
    137. End If
    138. End With
    139. End Sub
    140.  
    141. Private Sub Command2_Click()
    142.   If Command2.Caption = "Search" Then
    143.         Command2.Caption = "Stop"
    144.         frmSubjects.Height = 6255
    145.         Frame1.Visible = True
    146.     Else
    147.         Command2.Caption = "Search"
    148.         frmSubjects.Height = 5040
    149.         Frame1.Visible = False
    150.     End If
    151.  
    152. End Sub
    153.  
    154.  
    155.  
    156. Private Sub Command4_Click()
    157. With RS
    158.     .Close
    159.    
    160.     RS.Open "Select * FROM Subjects"
    161.    
    162.     .Requery
    163. End With
    164.  
    165. End Sub
    166.  
    167. Private Sub Form_Load()
    168.     CN.Open "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & App.Path & "\db1.mdb"
    169.     RS.CursorLocation = adUseClient
    170.     RS.Open "SELECT * FROM Subjects ORDER BY Subject ASC", CN, adOpenStatic, adLockOptimistic
    171.     Set DataGrid1.DataSource = RS
    172.    
    173.       With RS
    174.         If Not .BOF Then
    175.             txtSubject.Text = .Fields("Subject")
    176.             txtNoOfBooks.Text = .Fields("NoOfBooks")
    177.             txtDays.Text = .Fields("IssueDays")
    178.             txtFine.Text = .Fields("Finecharge")
    179.             txtReverse.Text = .Fields("ReserveCharge")
    180.            
    181.              lblStatus.Caption = "Total Records: " & CStr(RS.RecordCount)
    182.         End If
    183.     End With
    184.  
    185.   DataGrid1.Columns(0).Caption = "Subject"
    186.   DataGrid1.Columns(1).Caption = "Issue Days"
    187.   DataGrid1.Columns(2).Caption = "No Of Books"
    188.   DataGrid1.Columns(3).Caption = "Fine Charge"
    189.   DataGrid1.Columns(4).Caption = "Reverse Charge"
    190. End Sub

  2. #2
    VB Guru ganeshmoorthy's Avatar
    Join Date
    Dec 2005
    Location
    Sharjah, United Arab Emirates
    Posts
    3,031

    Re: Datagrid Help

    VB Code:
    1. 'In the click event of the datagrid paste these lines
    2.     DataGrid1.Row = Datagrid1.Row
    3.     DataGrid1.Col = 1 'Change the column no if this doesnt contain Subject
    4.     txtSubject.Text = DataGrid1.Text
    5.     DataGrid1.Col = 2 'Change the column no if this doesnt contain NoofBooks
    6.     txtNoOfBooks.Text = DataGrid1.Text
    7.     DataGrid1.Col = 3 'Change the column no if this doesnt contain IssueDays
    8.     txtDays.Text = DataGrid1.Text
    9.     DataGrid1.Col = 4 'Change the column no if this doesnt contain FineCharge
    10.     txtFine.Text = DataGrid1.Text
    11.     DataGrid1.Col = 5 'Change the column no if this doesnt contain ReserveCharge
    12.     txtReverse.Text = DataGrid1.Text
    If an answer to your question has been helpful, then please, Rate it!

    Have done Projects in Access and Member management systems using BioMetric devices, Smart cards and BarCodes.


  3. #3

    Thread Starter
    Frenzied Member
    Join Date
    Jul 2004
    Posts
    1,202

    Re: Datagrid Help

    The code below dosent work.... What i need is for when a client clicks on the datagrid and they select what record to view it then shows that record thats been clicked in the text boxes. All the code does is change the data.?

    VB Code:
    1. Private Sub Datagrid1_Click()
    2. Datagrid1.Row = Datagrid1.Row
    3. Datagrid.col = 1
    4. txtSubject.text = Datagrid1.text
    5. Datagrid.col = 2
    6. txtNoOfBooks.text = Datagrid1.text
    7. Datagrid.col = 3
    8. txtDays.text = Datagrid1.text
    9. Datagrid.col = 4
    10. txtFine.text = Datagrid1.text
    11. Datagrid.col = 5
    12. txtReserve.text = Datagrid1.text
    13. End Sub
    Last edited by Jamie_Garland; Dec 4th, 2006 at 09:53 AM.

  4. #4
    VB Guru ganeshmoorthy's Avatar
    Join Date
    Dec 2005
    Location
    Sharjah, United Arab Emirates
    Posts
    3,031

    Re: Datagrid Help

    What is it doing currently when you click on the data grid.
    If an answer to your question has been helpful, then please, Rate it!

    Have done Projects in Access and Member management systems using BioMetric devices, Smart cards and BarCodes.


  5. #5
    PowerPoster
    Join Date
    Nov 2002
    Location
    Manila
    Posts
    7,629

    Re: Datagrid Help

    If your gonna be using RS to navigate via the datagrid then DON'T close the recordset, repopulating with new records as this clears your previous list.

    If your using the textboxes to edit the values then you shouldn't be using the datagrid. You might want to consider transfering the selection list to a listview in report view... you can resort the rows per column in a listview, this might come in handy. But for the listview implementation to work you need to have a primary key in your recordset. When a listitem is clicked, get the primary key (stored in the tag property of the listitem) and use search/find method on the RS based on primary key... current record would be updated and you can transfer from RS to the textboxes.

    The only drawback is you also have to maintain the listview, if a record is edited then you will also have to update the listview... if a record is added then you will also have to add a listitem... if a record is deleted then you will have to delete the listitem from the listview (the listview has a find method which you can use).

  6. #6
    VB Guru ganeshmoorthy's Avatar
    Join Date
    Dec 2005
    Location
    Sharjah, United Arab Emirates
    Posts
    3,031

    Re: Datagrid Help

    Jamie: try this,
    VB Code:
    1. Set txtSubject.DataSource = Adodc1.Recordset
    2.     txtSubject.DataField = Adodc1.Recordset.Fields("Subject").Name
    3.  
    4.     Set txtNoOfBooks.DataSource = Adodc1.Recordset
    5.     txtNoOfBooks.DataField = Adodc1.Recordset.Fields("NoOfBooks").Name
    6.  
    7.     '...bind the same way for other text boxes too
    8.     'And delete all the codes in the DataGrid1_Click()
    If an answer to your question has been helpful, then please, Rate it!

    Have done Projects in Access and Member management systems using BioMetric devices, Smart cards and BarCodes.


  7. #7

    Thread Starter
    Frenzied Member
    Join Date
    Jul 2004
    Posts
    1,202

    Re: Datagrid Help

    It changes all the data around and then it dosent work

  8. #8
    VB Guru ganeshmoorthy's Avatar
    Join Date
    Dec 2005
    Location
    Sharjah, United Arab Emirates
    Posts
    3,031

    Re: Datagrid Help

    Im sorry, I dint mention properly, you have to copy this code into your Form_Load() event and delete all the codes there in the DataGrid1_click() event.
    If an answer to your question has been helpful, then please, Rate it!

    Have done Projects in Access and Member management systems using BioMetric devices, Smart cards and BarCodes.


  9. #9

    Thread Starter
    Frenzied Member
    Join Date
    Jul 2004
    Posts
    1,202

    Re: Datagrid Help

    This code still dosent work..

    VB Code:
    1. Private Sub Form_Load()
    2.  
    3.     CN.Open "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & App.Path & "\db1.mdb"
    4.     RS.CursorLocation = adUseClient
    5.     RS.Open "SELECT * FROM users ORDER BY uname ASC", CN, adOpenStatic, adLockOptimistic
    6.     Set DataGrid1.DataSource = RS
    7.  
    8.  
    9.     With RS
    10.         If Not .BOF Then
    11.             txtUser.Text = .Fields("uname")
    12.             txtPass.Text = .Fields("Pass")
    13.         End If
    14.     End With
    15.  
    16.     DataGrid1.Columns(0).Caption = "Username"
    17.     DataGrid1.Columns(1).Caption = "Password"
    18.    
    19.     DataGrid1.Row = DataGrid1.Row
    20.     DataGrid1.Col = 0 'Change the column no if this doesnt contain Subject
    21.     txtUser.text = DataGrid1.Text
    22.     DataGrid1.Col = 1 'Change the column no if this doesnt contain NoofBooks
    23.     txtPass.Text = DataGrid1.Text
    24.  
    25.  
    26.  
    27. End Sub
    Last edited by Jamie_Garland; Dec 6th, 2006 at 05:47 PM.

  10. #10
    Hyperactive Member
    Join Date
    Dec 2006
    Location
    Jar, Norway
    Posts
    372

    Re: Datagrid Help

    I tried a testApp and it worked, but the TextBoxes did not take the changes before I DblClicked. Strange, but whether I moved the
    VB Code:
    1. DataGrid1.Row = DataGrid1.Row
    2.     DataGrid1.Col = 0 'Change the column no if this doesnt contain Subject
    3.     txtUser.text = DataGrid1.Text
    4.     DataGrid1.Col = 1 'Change the column no if this doesnt contain NoofBooks
    5.     txtPass.Text = DataGrid1.Text
    to MouseDown or MouseUp a DblClick was needed. Maybe DataGrid1.SetFocus in MouseDown and the above code in Click will do?
    Morten

  11. #11

    Thread Starter
    Frenzied Member
    Join Date
    Jul 2004
    Posts
    1,202

    Re: Datagrid Help

    I tryed that but when i double click on the record and it changed the persons details..

  12. #12
    Hyperactive Member
    Join Date
    Dec 2006
    Location
    Jar, Norway
    Posts
    372

    Re: Datagrid Help

    You are quite right.
    I changed the DataGrid1 with a MSHFlexGrid named DataGrid1.
    Then it workes with the following code:
    VB Code:
    1. Private Sub DataGrid1_Click()
    2. 'In the click event of the datagrid paste these lines
    3.     DataGrid1.Row = DataGrid1.Row
    4.     DataGrid1.Col = 1 'Change the column no if this doesnt contain Subject
    5.     txtSubject.Text = DataGrid1.Text
    6.     DataGrid1.Col = 2 'Change the column no if this doesnt contain NoofBooks
    7.     txtNoOfBooks.Text = DataGrid1.Text
    8.     DataGrid1.Col = 3 'Change the column no if this doesnt contain IssueDays
    9.     txtDays.Text = DataGrid1.Text
    10.     DataGrid1.Col = 4 'Change the column no if this doesnt contain FineCharge
    11.     txtFine.Text = DataGrid1.Text
    12.     DataGrid1.Col = 5 'Change the column no if this doesnt contain ReserveCharge
    13.     txtReverse.Text = DataGrid1.Text
    14. End Sub
    15.  
    16. Private Sub DataGrid1_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
    17. '  DataGrid1.SetFocus
    18. 'In the click event of the datagrid paste these lines
    19.     DataGrid1.RowSel = DataGrid1.Row
    20. End Sub
    21.  
    22. Private Sub Form_Load()
    23.     CN.Open "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & App.Path & "\db1.mdb"
    24.     RS.CursorLocation = adUseClient
    25.     RS.Open "SELECT * FROM Subjects ORDER BY Subject ASC", CN, adOpenStatic, adLockOptimistic
    26.  
    27.     Set DataGrid1.DataSource = RS
    28.    
    29.       With RS
    30.         If Not .BOF Then
    31.             txtSubject.Text = .Fields("Subject")
    32.             txtNoOfBooks.Text = .Fields("NoOfBooks")
    33.             txtDays.Text = .Fields("IssueDays")
    34.             txtFine.Text = .Fields("Finecharge")
    35.             txtReverse.Text = .Fields("ReserveCharge")
    36.            
    37.              lblStatus.Caption = "Total Records: " & CStr(RS.RecordCount)
    38.         End If
    39.     End With
    40.  
    41.   DataGrid1.ColHeaderCaption(0, 0) = "Subject"
    42.   DataGrid1.ColHeaderCaption(0, 1) = "Issue Days"
    43.   DataGrid1.ColHeaderCaption(0, 2) = "No Of Books"
    44.   DataGrid1.ColHeaderCaption(0, 3) = "Fine Charge"
    45.   DataGrid1.ColHeaderCaption(0, 4) = "Reverse Charge"
    46. End Sub

    Hope you manage to make this run for you too ... Morten

  13. #13
    PowerPoster
    Join Date
    Oct 2002
    Location
    British Columbia
    Posts
    9,758

    Re: Datagrid Help

    The row does not change until after the Click event. Use the DataGrid RowColChange event.

    VB Code:
    1. Private Sub DataGrid1_RowColChange(LastRow As Variant, ByVal LastCol As Integer)
    2.     If Not IsEmpty(LastRow) Then 'dont do anything unless the row changes
    3.         txtUser.Text = DataGrid1.Columns(0).CellText(DataGrid1.Bookmark)
    4.         txtPass.Text = DataGrid1.Columns(1).CellText(DataGrid1.Bookmark)
    5.     End If
    6. End Sub

  14. #14

    Thread Starter
    Frenzied Member
    Join Date
    Jul 2004
    Posts
    1,202

    Re: Datagrid Help

    Thanks now that we have that sorted can you tell me how to get it so when i search for a member and so it only shows the record in the datagrid.

  15. #15
    PowerPoster
    Join Date
    Oct 2002
    Location
    British Columbia
    Posts
    9,758

    Re: [RESOLVED] Datagrid Help

    Close the Recordset, Open it with a new query, Rebind the DataGrid to the Recordset

    VB Code:
    1. Private Sub cmdSearch_Click()
    2.     RS.Close
    3.     RS.Open "Select * FROM Subjects WHERE subject ='" & txtSearch.Text & "'"
    4.     DataGrid1.ReBind
    5. End Sub

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width