Results 1 to 13 of 13

Thread: Update a RS using a flexgrid

  1. #1

    Thread Starter
    Fanatic Member holly's Avatar
    Join Date
    Aug 2002
    Location
    Somewhere on earth
    Posts
    721

    Update a RS using a flexgrid

    Hi

    I have a form that contains a flexgrid and a cmd button which the
    user presses to update any changes...but I have a problem
    everytime I press the update cmd I receive an error (see JPG
    attached)

    The code I use is
    [Highlight=VB]
    Private Sub CmdUpdate_Click()
    UpdateFlexreview

    End Sub
    Private Sub UpdateFlexreview()
    Dim i As Integer
    Dim j As Integer

    With ObjRsFlex
    For i = 1 To MSFlexReview.Rows - 1
    .Fields("Event_Datetime").Value = i
    For j = 1 To MSFlexReview.Cols - 1
    .Fields("Column" & j).Value = MSFlexReview.TextMatrix(i, j)
    Next j
    .Update
    Next i
    End With

    End Sub
    [/vbode]

    Has anyone any suggestions... the error is displayed on the
    bolded line!!!!
    Attached Images Attached Images  
    ** HOLLY **

  2. #2
    I'm about to be a PowerPoster! mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,170
    Show the code you're using to open the connection and recordset

  3. #3

    Thread Starter
    Fanatic Member holly's Avatar
    Join Date
    Aug 2002
    Location
    Somewhere on earth
    Posts
    721
    OK here goes! here is the procedure I use to populate the flexgrid and open the RS...

    VB Code:
    1. Dim strDatabase As String
    2.     Dim strUsername As String
    3.     Dim strPassword As String
    4.     Dim strServer As String
    5.     Dim objINIFile As cInifile
    6.     Dim strMfgDate As Date
    7.     'inititate new object
    8.     Set objINIFile = New cInifile
    9.    
    10.    On Error GoTo Form_Load_Error
    11.        
    12.    'Locate ini file and receive databasename/Username and Pwd
    13.    objINIFile.Path = App.Path & "\GOPALS.ini"
    14.    objINIFile.Section = "DataBase Parameters"
    15.    objINIFile.Key = "SERVER"
    16.    strServer = objINIFile.Value
    17.    objINIFile.Key = "DatabaseName"
    18.    strDatabase = objINIFile.Value
    19.    objINIFile.Key = "USERNAME"
    20.    strUsername = objINIFile.Value
    21.    objINIFile.Key = "PASSWORD"
    22.    strPassword = objINIFile.Value
    23.      
    24.     txtEdit.ZOrder
    25.     txtEdit = ""
    26.    
    27.     set_title_caption ' set title caption for MSFLex
    28.     enlarge_grid      ' Enlarge the grid to fit detail
    29.         If Time > Format(CDate("06:30:00"), "HH:MM:SS") Then
    30.            lbldate = (Format(Now - 1, "MMM DD, YYYY"))
    31.             Else
    32.          lbldate = (Format(Now, "MMM DD, YYYY"))
    33.     End If
    34.          strMfgDate = lbldate ' assign the label date to a variable
    35.          lbltime = Time 'assign time to label
    36.          Tmrclock.Interval = 1000
    37.          Tmrclock.Enabled = True
    38.     MSFlexReview.FillStyle = 0 'format individual cells
    39.     txtEdit.Visible = False
    40.         DTStartDate = Now - 3 'Display start date
    41.         DTEnddate = DTStartDate + 7
    42.         'Opens Subroutine - connection to SQL with the values received from the ini file
    43.        Call OpenConnection(strServer, strUsername, strPassword, strDatabase)
    44.    Exit Sub
    45.    
    46. Private Sub DisplayResults()
    47.      'Declare variable
    48.       On Error GoTo Display_Result_Error
    49.     Dim IntCounter As Integer
    50.     Dim objCmd As ADODB.Command
    51.     Dim PrmBeginDate As ADODB.Parameter
    52.     Dim PrmEndDate As ADODB.Parameter
    53.     Dim startDate As Date
    54.     Dim endDate As Date
    55.  
    56.     startDate = DTStartDate.Value
    57.     endDate = DTEnddate.Value
    58.    
    59.     'Set counter to zero
    60.     IntCounter = 0
    61.     Set ObjRsFlex = New Recordset
    62.     Set objCmd = New ADODB.Command
    63.  
    64.    objOcn.CursorLocation = adUseClient
    65.    ObjRsFlex.LockType = adLockBatchOptimistic
    66.        
    67.         With objCmd
    68.             .ActiveConnection = objOcn
    69.             .CommandType = adCmdStoredProc
    70.             .CommandText = "SP_ReviewScale_Transactions"
    71.             .Parameters.Append .CreateParameter("@beginningDate", adDBDate, adParamInput, , Format(DTStartDate.Value, "yyyy-mmm-dd"))
    72.             .Parameters.Append .CreateParameter("@EndingDate", adDBDate, adParamInput, , Format(DTEnddate.Value, "yyyy-mmm-dd"))
    73.         End With
    74.  
    75.   'Populate grid with search results and set # rows to
    76.     'equal the # records returned by the search
    77.       Set ObjRsFlex = objCmd.Execute
    78.    
    79.     If ObjRsFlex.RecordCount > 0 Then
    80.           IntCounter = IntCounter + 1           'Reserves first row for column headers
    81.          MSFlexReview.Rows = IntCounter + 1       'Adds a row for next record
    82.  
    83.         While Not ObjRsFlex.EOF
    84.             With MSFlexReview
    85.                 .Row = 0
    86.                 .AddItem ObjRsFlex.Fields("Event_DateTime") & vbTab & ObjRsFlex.Fields("Operator_Badge") & vbTab & ObjRsFlex.Fields("Operator_Lastname") & vbTab & ObjRsFlex.Fields("Pallet_ID_Number") & vbTab & ObjRsFlex.Fields("Product_Code") & vbTab & ObjRsFlex.Fields("Product_description") & vbTab & ObjRsFlex.Fields("Product_Gross_Weight") & vbTab & ObjRsFlex.Fields("NET_WEIGHT")
    87.              End With
    88.             ObjRsFlex.MoveNext
    89.         Wend
    90.     Else
    91.         MsgBox " There is no records to populate the Grid."
    92.     End If
    93.     'clean objects
    94.     Set objCmd = Nothing
    95.     Set PrmBeginDate = Nothing
    96.     Set PrmEndDate = Nothing
    97.  
    98.    Exit Sub
    99.    
    100. Display_Result_Error:
    101.      Call frmscan.ShowError("Error -  in procedure Form_Load of Form frmscan")
    102. End Sub

    Thanks
    Last edited by holly; Jul 7th, 2004 at 05:29 AM.
    ** HOLLY **

  4. #4
    Addicted Member
    Join Date
    Jun 2002
    Location
    Brugge, Belgium
    Posts
    208
    Are you updating the whole recordset or just one row.

    In the latter case you'll have to capture the row or some sort of ID that you can use to populate a recordset.

    For example :

    flexgrid.col=1
    id=flexgrid.text

    with this id you can create a recordset wth only that one record and update it with data from the flexgrid or textboxes.

  5. #5

    Thread Starter
    Fanatic Member holly's Avatar
    Join Date
    Aug 2002
    Location
    Somewhere on earth
    Posts
    721
    Hi

    I wanted to update the RS the reason being..a user selects a
    specific date range which can display multiple records...

    second if you were to read my first post I wanted to update
    based upon the event_dattime the reason..every transaction has
    a different time...
    VB Code:
    1. Fields("Event_Datetime").Value = i

    Thanks
    ** HOLLY **

  6. #6
    Addicted Member
    Join Date
    Jun 2002
    Location
    Brugge, Belgium
    Posts
    208
    I'm not sure if it's clear to me.

    the user selects a range of records based on time.

    You then update this time field with a row number ?

    The error message you get might be the locktype. Or the stored procedure. The recordset might not be update-able. THen you'll have to loop the result set and update each record separately using some sort of ID as I mentioned earlier.

    set ObjRSFlex=new recordset

    shouldn't this be

    set objrsflex=new ADODB.recordset ?

  7. #7

    Thread Starter
    Fanatic Member holly's Avatar
    Join Date
    Aug 2002
    Location
    Somewhere on earth
    Posts
    721
    To answer your questions:

    The user selects a range of records based on date...see attached
    *.jpg
    I dont update anything because it does not work....

    Thanks

    Hope it is a bit clearer

    What I want to do is to be able to change any of the records
    and then update it back to SQL using the first post procedure..
    Last edited by holly; Jul 7th, 2004 at 08:32 AM.
    ** HOLLY **

  8. #8
    Addicted Member
    Join Date
    Jun 2002
    Location
    Brugge, Belgium
    Posts
    208
    The selection is clear but what do you want to update ?

    With what dou you want to update the selected data ?

    1. User selects a range
    2. User updates this range ? with what ?


    It's hard to see what you want the cmdupdate button to do.

    The only thing you're doing now is looping the rows and columns and that's it ? With a flexgrid there's no link between data and display. A data grid might be more appropriate but is less flexible (according to me).

  9. #9

    Thread Starter
    Fanatic Member holly's Avatar
    Join Date
    Aug 2002
    Location
    Somewhere on earth
    Posts
    721
    I want to update with anything I wish to change....so for
    example if I want to change the Operators_lastname from
    'harrison' to 'harris'.

    I'm sorry if I am not making myself clear enough

    Thanks for you help!!!

    ** HOLLY **

  10. #10
    Addicted Member
    Join Date
    Jun 2002
    Location
    Brugge, Belgium
    Posts
    208
    No problem.

    It's always better to state the full idea than only a small part.

    The only way to change data in this range is by looping the range and editing record by record.
    You can have a combo which contains the different fields that are displayed. Next to this you can put a textbox.

    When the user selects "lastname" the user enters the new value into the textbox and clicks update.

    With the textbox data you should perform an update for each record that is displayed in the flexgrid.

    therefore you'll be using the for.. next loop. And as you know that the user selected lastname, you know what field to update.

    You can perform a similar action on the click event of the flexgrid. If the user clicks the lastname column you can pre-select lastname in the combo and set the text from the flexgrid into the textbox. Then edit the textbox and click update and that's it.

    The recordset has to be opened in such a way so you can update it.

  11. #11

    Thread Starter
    Fanatic Member holly's Avatar
    Join Date
    Aug 2002
    Location
    Somewhere on earth
    Posts
    721
    I understand what youre saying is just that I'm stuck with
    the code...Do i just add a locktype in the displayresults procedure
    or do I need to do a bit more??

    Thanks
    ** HOLLY **

  12. #12
    Addicted Member
    Join Date
    Jun 2002
    Location
    Brugge, Belgium
    Posts
    208
    Code:
    objOcn.CursorLocation = adUseClient
       ObjRsFlex.LockType = adLockBatchOptimistic
    You can try to change the adlockBatchOptimistic into adLockOptimistic ?

    I presume you're using SQL Server ? I'm not sure if adlockoptimistic exists for this MS SQL. Have you changed the new recordset into new adodb.recordset ?

    I think without adodb you're creating a DAO recordset.

    The only way to change the items in the flexgrid is doing it line by line. using the date_time stamp in combination with the field that needs to be updated.
    Code:
    with flexgrid
    .col=1
    DatetimeStamp=.text
    .col=2
    lastname=.text
    end with
    
    set adors=new adodb.recordset
    adors.open "select * from sales where timestamp=" datetimestamp & " and employee.lastname= ' " & lastname &"'"
    
    if not adors.eof and not adors.bof then
    adors!lastname=txtEditedLastname.text
    end if
    You could also create a stored procedure that receives an sql string. But I'm not familiar with stored procedures.

  13. #13
    PowerPoster
    Join Date
    Oct 2002
    Location
    British Columbia
    Posts
    9,758
    The Command.Execute method can only create a ReadOnly recordset. Use the Recordset.Open method instead.

    Change
    Set ObjRsFlex = objCmd.Execute

    To
    ObjRsFlex.Open objCmd, , adOpenStatic, adLockOptimistic

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