Results 1 to 14 of 14

Thread: DataGrid update DB not working as it should

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    Dec 2007
    Location
    West Yorkshire, UK
    Posts
    791

    DataGrid update DB not working as it should

    I have a DataGrid which is bound to an ADODC and it allows updates. The database consists of different tables and the SQL for this ADODC is "SELECT stID, stName, grID, grName FROM tblStudent, tblGroup WHERE tblGroup.grID=tblStudent.stGroup ORDER BY grName, stName". The student's group is referenced to the Group table Key field.
    I want to be able to easily assign a student to a group by changing the Group column in the DataGrid. However, it changes the name of the group in tblGroup instead of the Group ID in tblStudent.
    I can overcome this by showing stGroup in the datagrid and the user enters the Group code (key) instead of the group name but that is VERY messy!
    In an ideal world I would have a combobox in the Group column and the user simply selects the correct group for that student.
    Please can anyone help me with this, how to fix it, or suggest a different approach - preferably not msFlexGrid because that looks very complicated since it does not allow updates.
    My code is below:
    vb Code:
    1. Private Sub FillGrid()
    2. Dim sSQL As String
    3.    sSQL = "SELECT stID, stName, grID, grName FROM tblStudent, tblGroup WHERE tblGroup.grID=tblStudent.stGroup"
    4.    sSQL = sSQL & " ORDER BY grName, stName"
    5.    ADOAllocate.RecordSource = sSQL
    6.    ADOAllocate.CommandType = adCmdText
    7.    ADOAllocate.Refresh
    8.    dgAllocate.AllowRowSizing = False
    9.    dgAllocate.Columns(0).Width = 0            'hide Key row
    10.    dgAllocate.Columns(1).Width = 2500
    11.    dgAllocate.Columns(1).Locked = True        'can't change student name
    12.    dgAllocate.Columns(2).Width = 0            'hide group code
    13.    dgAllocate.Columns(3).Width = 900          'Group
    14.    dgAllocate.ScrollBars = dbgHorizontal
    15. End Sub

  2. #2

    Thread Starter
    Fanatic Member
    Join Date
    Dec 2007
    Location
    West Yorkshire, UK
    Posts
    791

    Re: DataGrid update DB not working as it should

    anybody? please?

  3. #3
    Member
    Join Date
    Nov 2012
    Posts
    62

    Re: DataGrid update DB not working as it should

    How I would approach your problem:
    As you only want to edit the group ID for the student (which you can't see), don't make the grid edittable. Instead, when the user clicks on the group, show a listbox/combobox/menu with the available groups. When the user makes a selection from there, run an SQL Update to set the groupID for that student and refresh the grid.

    Probably not the slickest solution, but it should work.

  4. #4
    PowerPoster SamOscarBrown's Avatar
    Join Date
    Aug 2012
    Location
    NC, USA
    Posts
    9,622

    Re: DataGrid update DB not working as it should

    Buenos Dias!
    May I recommend you move away from the ADODC control, and start using other controls to hold/display your database information (MSFlexgrids, listboxes, textboxes, etc) which you should get using ADO (and definitely not the old DAO) structured queries. You will find a LOT more immediate help with your projects. I'd recommend you start with THIS project, and rewrite it. It will save you lots of headaches down the road.

  5. #5
    gibra
    Guest

    Re: DataGrid update DB not working as it should

    Quote Originally Posted by Españolita View Post
    I have a DataGrid which is bound to an ADODC and it allows updates.
    ---/---
    I can overcome this by showing stGroup in the datagrid and the user enters the Group code (key) instead of the group name but that is VERY messy!
    ---/---
    In an ideal world I would have a combobox in the Group column and the user simply selects the correct group for that student.
    1) In you SQL statement you must add ALL columns you need to update, eventually hide columns that user doesn't to access it.
    2) you must use appropriate DataGrid events named After* and Before* to manage data, so you can accept or reject values entered by user.

    3) Doesn't exists a 'bound' ComboBox to DataGrid (as in ComponentOne TrueDBGrid control), so if you want to use a ComboBox then you have to manage it by code using a hidden control, see:
    How to Place a Combo in DataGrid in VB6
    http://www.ehow.com/how_10031164_pla...agrid-vb6.html

    4) I strongly recommend you to leave the ADODC control and replace it with the ADODB object (code only), much more efficient and bug-free.

  6. #6

    Thread Starter
    Fanatic Member
    Join Date
    Dec 2007
    Location
    West Yorkshire, UK
    Posts
    791

    Re: DataGrid update DB not working as it should

    First, thank you for your replies.
    OK, ADODC has been ditched. My new code now also includes the reference to the group code in the Student table:
    vb Code:
    1. Dim sSQL As String
    2.    sSQL = "SELECT stID, stName, stGroup, grID, grName FROM tblStudent, tblGroup WHERE tblGroup.grID=tblStudent.stGroup"
    3.    sSQL = sSQL & " ORDER BY grName, stName"
    4.    Set CnAllocate = New ADODB.Connection
    5.    CnAllocate.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;" & _
    6.                        "Data Source=" & App.Path & "\Student.mdb;Persist Security Info=False"
    7.    CnAllocate.Open
    8.    Set RsAllocate = New ADODB.Recordset
    9.    RsAllocate.CursorLocation = adUseClient
    10.    RsAllocate.Open sSQL, cn, adOpenKeyset, adLockPessimistic, adCmdText
    11.    Set dgAllocate.DataSource = RsAllocate
    12.    dgAllocate.Refresh
    13.    dgAllocate.AllowRowSizing = False
    14.    dgAllocate.Columns(0).Width = 0            'hide Key row
    15.    dgAllocate.Columns(1).Width = 2500
    16.    dgAllocate.Columns(1).Locked = True        'can't change student name
    17.    dgAllocate.Columns(2).Width = 0            'hide group code
    18.    dgAllocate.Columns(3).Width = 0            'hide group code in Student table
    19.    dgAllocate.Columns(4).Width = 900          'Group
    20.    dgAllocate.ScrollBars = dbgHorizontal

    Gibra. I had already found that link you gave me. The problem is that step 3 states:
    Get the top position, row height and column width of the cell on "DataGrid1"
    It doesn't say how to do that and searching has now given me anything. I tried to get the co-ordinates of the clicked cell as a starting point with this code:
    vb Code:
    1. Private Sub dgAllocate_Click()
    2. Dim Rw As Integer
    3. Dim Cl As Integer
    4. Rw = dgAllocate.Row
    5. Cl = dgAllocate.Col
    6. MsgBox "Row:" & Rw & "  Col:" & Cl
    7. End Sub
    The problem with this is that is gives the row and column number of the PREVIOUS cell clicked on.
    Can anyone give me guidance on how to follow Step 3 as mentioned above?

  7. #7
    gibra
    Guest

    Re: DataGrid update DB not working as it should

    You don't use the Click event, which is fired for DataGrid control only, not for records collection.
    As already sayed, use After* and Before* events, their allows you to stop/cancel/modify values entered by user Before and/or After the entered values.
    Eventually, use the RowColChange event, also, but maybe secondary.

  8. #8
    PowerPoster SamOscarBrown's Avatar
    Join Date
    Aug 2012
    Location
    NC, USA
    Posts
    9,622

    Re: DataGrid update DB not working as it should

    Lo siento, Esp....
    If I were using an MSFlexGrid instead of a bound datagrid, this would be easy. I would simply click on the field in the MSFlexGrid, noting it's row and column (textmatrix(rownum, colnum)), and record that value (like your grId/stgroup), and THEN, I'd write an update query replacing those values in both tables. I simply don't use datagrids (not because they are not functional, but my experience has been (for MANY years) with flexgrids). I wish I could advise on your datagrid--I DO recognize your issue, but can't offer any good advice (except maybe using an MSFlexgrid--~smile~).
    I am sure someone will assist you soonest.

  9. #9

    Thread Starter
    Fanatic Member
    Join Date
    Dec 2007
    Location
    West Yorkshire, UK
    Posts
    791

    Re: DataGrid update DB not working as it should

    gracias Sam,
    I tried adding MSFlexGrid but I couldn't attach it to my ADODB.Some checking online showed I need an MSHFlexGrid for that, so I installed that. I can now click on a cell and get it to show a ComboBox over that cell. I need guidance now, with how to take the value selected from the ComboBox, update the DataBase and refresh the HFlexGrid (because it doesn't allow editing.
    My relevant code is below.:
    vb Code:
    1. Private Sub FillGrid()
    2. Dim sSQL As String
    3.    sSQL = "SELECT stID, stName, stGroup, grID, grName FROM tblStudent, tblGroup WHERE tblGroup.grID=tblStudent.stGroup"
    4.    sSQL = sSQL & " ORDER BY grName, stName"
    5.    Set CnAllocate = New ADODB.Connection
    6.    CnAllocate.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;" & _
    7.                        "Data Source=" & App.Path & "\Student.mdb;Persist Security Info=False"
    8.    CnAllocate.Open
    9.    Set RsAllocate = New ADODB.Recordset
    10.    RsAllocate.CursorLocation = adUseClient
    11.    RsAllocate.Open sSQL, cn, adOpenKeyset, adLockPessimistic, adCmdText
    12.    Set hfgAllocate.DataSource = RsAllocate
    13.    hfgAllocate.Refresh
    14.    hfgAllocate.ColWidth(0) = 0
    15.    hfgAllocate.ColWidth(1) = 0               'hide key row
    16.    hfgAllocate.ColWidth(2) = 3500            'show name
    17.    hfgAllocate.ColWidth(3) = 0               'Hide group code
    18.    hfgAllocate.ColWidth(4) = 0               'Hide group code in student table
    19.    hfgAllocate.ColWidth(5) = 1200             'show Group name
    20.    hfgAllocate.ScrollBars = flexScrollBarHorizontal
    21. End Sub
    22.  
    23. Private Sub hfgAllocate_Click()
    24.    If hfgAllocate.Col = 5 Then
    25.       Combo1.Top = hfgAllocate.CellTop + hfgAllocate.Top - 30
    26.       Combo1.Left = hfgAllocate.CellLeft + hfgAllocate.Left
    27.       Combo1.Width = hfgAllocate.CellWidth - 15
    28.       Combo1.Visible = True
    29.    End If
    30. End Sub

    ps. Spanish is not necessary. I'm English, I just happen to live in Spain

  10. #10
    gibra
    Guest

    Re: DataGrid update DB not working as it should

    Quote Originally Posted by SamOscarBrown View Post
    Lo siento, Esp....
    If I were using an MSFlexGrid instead of a bound datagrid, this would be easy. I would simply click on the field in the MSFlexGrid, noting it's row and column (textmatrix(rownum, colnum)), and record that value (like your grId/stgroup), and THEN, I'd write an update query replacing those values in both tables. I simply don't use datagrids (not because they are not functional, but my experience has been (for MANY years) with flexgrids). I wish I could advise on your datagrid--I DO recognize your issue, but can't offer any good advice (except maybe using an MSFlexgrid--~smile~).
    I am sure someone will assist you soonest.
    My experience is:

    1 - I don't use any flex grid (MsFlexGrid, MsHFlexGrid, VSFlexGrid, ...). I use ComponentOne TrueDBGrid. If I had not the TrueDBGrid I would use the DataGrid.
    I used the FlexGrid for my customer, but it was a suffering, accustomed to using the TrueDBGrid!
    Because of the integration between ADODB.Recordset and DataGrid/TrueDBGridis excellent.
    When I change a value in the source (ADODB.Recordset) by code, it is automatically reflected in the DataGrid.
    FlexGrid dosn't have a prepared methods to refresh data, as Requery and Resync, not to mention the performance for Filter, Find that in the any flexgrid are completely absent, and finally the Sort of flexgrid is not even comparable to the Sort of DataGrid .
    I understand that the FlexGrid can have merged cells, colored, etc.. but these do not justify the lack of advanced features which instead has the DataGrid.


    2 - I don't allow row-edit, never.
    User must double-click the row to open a Form for Add or Edit record.
    The data entry is more easy, by user, because into a Form I can use any type of specialized control (for numbers, dates, combobox, ...)
    I can verify/check the data much better.
    After saved, any changes are reflected instantly into DataGrid, simply calling Requery method.

    Forgive me, but there is no comparison when working with data (database).

  11. #11

    Thread Starter
    Fanatic Member
    Join Date
    Dec 2007
    Location
    West Yorkshire, UK
    Posts
    791

    Re: DataGrid update DB not working as it should

    OK, I have now got to the stage where I get the StudentID from column 1 of the clicked row in my HFG and I get the GroupID from the ItemData in the Combobox. I update the cell in my HFG with combobox.Text but this obviously does not update the Access DB.

    My question now is, how do I update the DB? Can I just use the StudentID, and update the single field (stGroup) or do I have to load ALL the fields in the Student Table, hide all the unnecessary 30 or so columns, then, when the user changes the group allocation, read back the info in all the columns for that row in the HFG and rewrite the entire record?

  12. #12
    PowerPoster SamOscarBrown's Avatar
    Join Date
    Aug 2012
    Location
    NC, USA
    Posts
    9,622

    Re: DataGrid update DB not working as it should

    Your first response to your question is correct. If you can get the value of the ONE StudentID you want to update, put the group id and name into variables and do a new update SQL
    e.g., update tblStudent set grId = myIDVariable, grName = myNameVariable where stID = myIDVariable
    do the same thing for the other table using appropriate variables

    You can do this on the click_event of your Grid. Now, I'm not sure how this will work with a bound grid....like I said before, I always use flexgrids and populate them with sql queries by going thorugh my recordcollection. I am sure there are plenty of folks out there who can help you with bound grids....I just don't use them.

    ANYONE? Doog, DMiser, Dilettante, MAx??????? (and I am missing a lot of the EXPERTS out there).

  13. #13

    Thread Starter
    Fanatic Member
    Join Date
    Dec 2007
    Location
    West Yorkshire, UK
    Posts
    791

    Re: DataGrid update DB not working as it should

    Thank you Sam, I have it working now. I didn't know about SQL SET.
    + rep for you.

  14. #14
    PowerPoster SamOscarBrown's Avatar
    Join Date
    Aug 2012
    Location
    NC, USA
    Posts
    9,622

    Re: DataGrid update DB not working as it should

    Gracias Esp.....glad you got it working......have a good night.

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