Results 1 to 27 of 27

Thread: to save the data in flexgrid

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Mar 2002
    Location
    faridabad
    Posts
    101

    to save the data in flexgrid

    hi,


    i am having one problem.i want to save the data in the database which i enter in the flexgrid.i am using ado

    i want that as soon as the flexgrid_leavecell event is invoked it saves the data in the database

    the coding of flexgrid_leavecell which i am using is::


    Private Sub flxgrid_LeaveCell()
    If Not IsNull(txtedit.Text) Then
    'user typed an entry in the text box
    flxgrid.TextMatrix(Active_FlxCell.row, Active_FlxCell.col) = txtedit.Text
    End If
    End Sub


    regards
    ishikha
    ishikha

  2. #2
    Randalf the Red honeybee's Avatar
    Join Date
    Jun 2000
    Location
    off others' brains
    Posts
    4,345

    Well ...

    Why would you want to do it in the LeaveCell event? A better approach could be to use a Save button outside the grid, which saves the entire grid's contents into the database.

    If you still want to do it in the LeaveCell() event, you already have the value to be saved using the TextMatrix method. Simply assign/insert it into the appropriate row in the appropriate column.

    Assuming each row of the grid corresponds to each row of the recordset, and each column of the grid corresponds to each column of the recordset, and all data are text type, you can write something like this:

    rs.Move grd.Row
    rs.Fields(grd.Col) = grd.TextMatrix(grd.Row, grd.Col)
    rs.Update

    If the data is not text, you can add appropriate formatting in the assignment statement above. Also you can add validation for the correct row value, so if your grid accepts new rows entered by the user, you can use AddNew.

    There could be many variations here, depending upon what you like.

    .
    I am not a complete idiot. Some parts are still missing.
    Check out the rtf-help tutorial
    General VB Faq Thread
    Change is the only constant thing. I have not changed my signature in a long while and now it has started to stink!
    Get more power for your floppy disks. ; View honeybee's Elite Club:
    Use meaningfull thread titles. And add "[Resolved]" in the thread title when you have got a satisfactory response.
    And if that response was mine, please think about giving me a rep. I like to collect them!

  3. #3
    I'm about to be a PowerPoster! mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,170
    You could also do it in the textbox validate event (which is fired when you leave the textbox)

    I've noticed this is a cross post.

    would you like help, or have you already gotten help?

  4. #4

    Thread Starter
    Lively Member
    Join Date
    Mar 2002
    Location
    faridabad
    Posts
    101

    to save the data in flexgrid

    hi,

    it's not neccesary to save the data after the leavecell event .
    can u tell me what code i have to use in order to save all the changes in the database by just clicking on save button.

    also one thing more when i add the new row and enter the data then how i will save it.

    i am also adding the new row in between the flexgrid.in that case i want that it saves that row in between the records in which i enter and not in the end.

    ishikha
    ishikha

  5. #5
    Randalf the Red honeybee's Avatar
    Join Date
    Jun 2000
    Location
    off others' brains
    Posts
    4,345

    Well ...

    First you need to use a primary key for each row of your recordset. This will help in identifying rows later when you are trying to save.

    When populating the flexgrid, at least in the beginning, use all the fields in the database, maintain a one-to-one correspondence, and remember that you have to manipulate the data types.

    The primary key, which is usually the first column, should ideally go to the first column of the grid, which you can set to be invisible, so the cryptic IDs won't be displayed to the user.

    While saving, you can use this ID, locate the record in the recordset. If found, edit it, else add a new one and save it. The code would then look like:

    Code:
    Dim I As Integer, J As Integer
    For I = 0 To grd.Rows - 1 Step 1
       rs.FindFirst "ID='" & grd.TextMatrix(I, 0) & "'"
       If rs.NoMatch Then
          rs.AddNew
       End If
       For J = 0 To grd.Cols - 1 Step 1
          rs(J) = grd.TextMatrix(I, J)  'Beware of data types
       Next
    Next
    Also, this is DAO code, so please make appropriate changes in ADO.

    .
    Last edited by honeybee; Apr 16th, 2002 at 04:49 AM.
    I am not a complete idiot. Some parts are still missing.
    Check out the rtf-help tutorial
    General VB Faq Thread
    Change is the only constant thing. I have not changed my signature in a long while and now it has started to stink!
    Get more power for your floppy disks. ; View honeybee's Elite Club:
    Use meaningfull thread titles. And add "[Resolved]" in the thread title when you have got a satisfactory response.
    And if that response was mine, please think about giving me a rep. I like to collect them!

  6. #6

    Thread Starter
    Lively Member
    Join Date
    Mar 2002
    Location
    faridabad
    Posts
    101

    to save the data in flexgrid

    hi,

    u want to say that i enter one more column in my table and made it a primary key.then what type of data i enter in that column

    presently i am having only three fields in the table .the datatypes of all the three fields are text.and none of the fields is primary.

    and also in ado there is no such property of findfirst

    ishikha
    ishikha

  7. #7

    Thread Starter
    Lively Member
    Join Date
    Mar 2002
    Location
    faridabad
    Posts
    101

    to save the data in flexgrid

    hi,

    plzz somebody tell me the solution of this .

    ishikha
    ishikha

  8. #8
    I'm about to be a PowerPoster! mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,170
    Honeyb is telling you to create a primary field in your database. without a primary key, you'll be facing problems.

    so go to your database, and make a primary key, such as an autonumber. Call that field ID.

    then try this.

    Code:
    Dim I As Integer, J As Integer
    For I = 0 To grd.Rows - 1 Step 1
       rs.Find "ID='" & grd.TextMatrix(I, 0) & "'"
       If rs.NoMatch Then
          rs.AddNew
       End If
       For J = 0 To grd.Cols - 1 Step 1
          rs(J) = grd.TextMatrix(I, J)  'Beware of data types
       Next
    Next

  9. #9

    Thread Starter
    Lively Member
    Join Date
    Mar 2002
    Location
    faridabad
    Posts
    101

    to save the data in flexgrid

    hi,

    ok i will add one more column in my table and made a primary key now i dinn' want to show on the form .how i will make it invisible

    and honeybee give the coding in dao can u plzz tell me what's the coding in ado

    regards
    ishikha
    ishikha

  10. #10
    I'm about to be a PowerPoster! mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,170

    Re: to save the data in flexgrid

    Originally posted by ishikha
    hi,

    ok i will add one more column in my table and made a primary key now i dinn' want to show on the form .how i will make it invisible

    and honeybee give the coding in dao can u plzz tell me what's the coding in ado

    regards
    ishikha
    You will make it invisible, by not showing it. so instead of
    objrs.fields(0).value you will just put objrs.fields(1).value so the field 0 gets skipped (field 0 is your primary key, usually)

    Make sure you add the primary key column as the very first column in your table.


    I have given you the ADO for it. It's just a bit different from DAO.

  11. #11

    Thread Starter
    Lively Member
    Join Date
    Mar 2002
    Location
    faridabad
    Posts
    101

    to save the data in flexgrid

    yes i make that field as the first column.

    i am asking u that how i will be able to hide the column first.when on the grid i click on retrieve structure it shows all the four fields then yhow i am able to hide at run time .

    and secondly i want that in field id it automatically generates the numbers as the new row is added at the end or if i add the new row in between then it will automatically adjusts the serial no

    ishikha
    ishikha

  12. #12
    I'm about to be a PowerPoster! mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,170

    Re: to save the data in flexgrid

    i am asking u that how i will be able to hide the column first.when on the grid i click on retrieve structure it shows all the four fields then yhow i am able to hide at run time .
    That's what I have answered. In your loop in which you are filling up your flexgrid, simply ignore the first field of your table. You MIGHT be having something like

    flexgrid.textmatris(1,1) = objrs.fields(0).value
    ....

    well, just make sure you never use the 0 (zero) there. It should start with 1 and go up to 3, or whatever you have. I hope you have understood this, you are simply to ignore the first field in your loop itself.

    and secondly i want that in field id it automatically generates the numbers as the new row is added at the end or if i add the new row in between then it will automatically adjusts the serial no
    Are you using SQL Server? if so, then in the design table mode, you click on identity seed. If you're using access, you simply make it an autonumber.

    However, it will not automatically adjust the serial number.

    If it's very very very important that you allow this functionality, then you will have to handcode the entire thing yourself.

  13. #13

    Thread Starter
    Lively Member
    Join Date
    Mar 2002
    Location
    faridabad
    Posts
    101

    to save the data in flexgrid

    u are not understanding what i am saying
    i dinn' want to display field id on the form at run time
    how i can do it

    and also the code u sent to me is not working
    it gives the error in adors.find statement saying that "type mismatch"
    and also in adors there is no such property of adors.nomatch



    Private Sub flxgrid_leavecell()
    Dim strtable1 As String
    Dim b As Integer
    Dim I As Integer, J As Integer
    flxgrid.ScrollBars = flexScrollBarBoth
    'Place scrollbars back onto the Flexgrid control.

    If Not txtedit.Text = "" Then
    'user typed an entry in the text box
    flxgrid.TextMatrix(Active_FlxCell.row, Active_FlxCell.col) = txtedit.Text

    For I = 0 To flxgrid.Rows - 1 Step 1
    adors.find "id='" & flxgrid.TextMatrix(I, 2) & "'"
    'If adors.NoMatch Then
    ' adors.AddNew
    'End If
    For J = 2 To flxgrid.Cols - 1 Step 1
    adors(J) = flxgrid.TextMatrix(I, J) 'Beware of data types
    Next
    Next
    end if
    end sub


    ishikha
    ishikha

  14. #14
    Randalf the Red honeybee's Avatar
    Join Date
    Jun 2000
    Location
    off others' brains
    Posts
    4,345

    Well ...

    Your grid must have ALL the columns from the database. To hide a column in the grid, set its width to zero.

    .
    I am not a complete idiot. Some parts are still missing.
    Check out the rtf-help tutorial
    General VB Faq Thread
    Change is the only constant thing. I have not changed my signature in a long while and now it has started to stink!
    Get more power for your floppy disks. ; View honeybee's Elite Club:
    Use meaningfull thread titles. And add "[Resolved]" in the thread title when you have got a satisfactory response.
    And if that response was mine, please think about giving me a rep. I like to collect them!

  15. #15

    Thread Starter
    Lively Member
    Join Date
    Mar 2002
    Location
    faridabad
    Posts
    101

    to save the data in flexgrid

    hi honeybee,

    the code which the mendhak gives for saving the data is not working it is giving the error can u see that and tell me what the error in that

    ishikha
    ishikha

  16. #16

    Thread Starter
    Lively Member
    Join Date
    Mar 2002
    Location
    faridabad
    Posts
    101

    to save the data in flexgrid

    hi honeybee,

    the code which the mendhak gives for saving the data is not working it is giving the error can u see that and tell me what the error in that

    ishikha
    ishikha

  17. #17
    I'm about to be a PowerPoster! mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,170
    I think we're having communication problems here. I will stop confusing you, ishikha.

    //gracefully bows out of post... for a while...


  18. #18
    Member
    Join Date
    Apr 2002
    Posts
    42
    pl check the post in general vb questions
    hope may help u

  19. #19
    Hyperactive Member vbud's Avatar
    Join Date
    Jan 2002
    Location
    Mru 20 17S, 57 33E Goal: Get out of the BOX Status: In The Shadows!!! Target Posts: 3,000,000,000
    Posts
    378

    Cool

    First of all when you are displaying your data set the first column width (The one where u r displaying your primary field) to 0. e.g :

    VB Code:
    1. MSFlexGrid1.ColWidth(0)

    Then use this piece of code to save your data (better with a save command button):-

    VB Code:
    1. Private Sub SaveRecords()
    2.  
    3.   Dim lcLngi        As Long
    4.   Dim lcStrSQL      As String
    5.   Dim lcFlgUpdate   As Boolean
    6.  
    7.   On Error GoTo ErrHandler
    8.  
    9.   With MSFlexGrid1
    10.    
    11.     For lcLngi = 1 To .Rows '* Or maybe its (Rows - 1)...
    12.          
    13.       lcStrSQL = "INSERT INTO [TableName goes here]([Your FieldNames goes here...]) " _
    14.                 & "VALUES('" & .TextMatrix(lcLngi, 1) & "','" _
    15.                 & .TextMatrix(lcLngi, 2) & "','" _
    16.                 & .TextMatrix(lcLngi, 3) & "')"
    17.                
    18.       '* Execute SQL statement using an ADO Connection
    19.       '* E.g: adoConnection.Execute lcStrSQL
    20.       If lcFlgUpdate Then
    21.         lcStrSQL = "UPDATE [TableName goes here]" _
    22.                  & "SET [FieldName1] = '" & .TextMatrix(lcLngi, 1) & "','" _
    23.                  & "[FieldName2] = '" & .TextMatrix(lcLngi, 2) & "','" _
    24.                  & "[FieldName3] = '" & .TextMatrix(lcLngi, 3) & "' " _
    25.                  & "WHERE [PrimaryKey] = " & CLng(.TextMatrix(lcLngi, 0))
    26.         '* Execute SQL statement using an ADO Connection
    27.         '* E.g: adoConnection.Execute lcStrSQL
    28.        
    29.       End If
    30.      
    31.     Next
    32.    
    33.   End With
    34.  
    35.   Exit Sub
    36.  
    37. ErrHandler:
    38.   '* Detect duplication error here - i don`t remember it...
    39.   If Err.Number = ["Duplication error"] Then
    40.     lcFlgUpdate = True
    41.     Resume Next
    42.   Else
    43.     MsgBox Err.Number & ": " & Err.Description
    44.   End If
    45.  
    46. End Sub
    >!v!<
    Free your mind, stop thinking
    http://inspirone.blogspot.com

    Please rate this post if it helped you

  20. #20

    Thread Starter
    Lively Member
    Join Date
    Mar 2002
    Location
    faridabad
    Posts
    101

    Red face to save the data in flexgrid

    hi,

    i dinn' able to understand ur code can u simplify it
    or u write the code acc to my table .

    the structure of my table is:

    my table name is document and it is having three fields

    1.folder 2.fileno 3.name

    all the three fields are of text type
    and the database i am using is access 97

    ishikha
    ishikha

  21. #21
    Member
    Join Date
    Apr 2002
    Posts
    42
    Problem solved ?

  22. #22
    Hyperactive Member vbud's Avatar
    Join Date
    Jan 2002
    Location
    Mru 20 17S, 57 33E Goal: Get out of the BOX Status: In The Shadows!!! Target Posts: 3,000,000,000
    Posts
    378

    Cool

    Ok here`s an amended version but you still have 2 use a primary key...

    VB Code:
    1. Private Sub SaveRecords()
    2.  
    3.   Dim lcLngi        As Long
    4.   Dim lcStrSQL      As String
    5.   Dim lcFlgUpdate   As Boolean
    6.   Dim lcLngErrLevel As Long
    7.   Dim lcObjConn     As ADODB.Connection
    8.   Dim lcStrConnString As String
    9.  
    10.   On Error GoTo ErrHandler
    11.  
    12.   Set lcObjConn = New ADODB.Connection
    13.   lcStrConnString = "Provider=Micorsoft.Jet.OLEDB.4.0 ...etc.."
    14.   lcObjConn.Open lcStrConnString
    15.  
    16.   With MSFlexGrid1
    17.    
    18.     lcLngErrLevel = 1000
    19.    
    20.     For lcLngi = 1 To .Rows '* Or maybe its (Rows - 1)...
    21.          
    22.       lcStrSQL = "INSERT INTO Document(Folder, FileNo, Name) " _
    23.                 & "VALUES('" & .TextMatrix(lcLngi, 1) & "','" _
    24.                 & .TextMatrix(lcLngi, 2) & "','" _
    25.                 & .TextMatrix(lcLngi, 3) & "')"
    26.                      
    27.       lcLngErrLevel = 1100
    28.       '* Execute SQL statement using an ADO Connection
    29.       lcObjConn.Execute lcStrSQL
    30.       lcLngErrLevel = 1200
    31.      
    32.       If lcFlgUpdate Then
    33.         lcLngErrLevel = 1300
    34.         lcStrSQL = "UPDATE Document " _
    35.                  & "SET Folder = '" & .TextMatrix(lcLngi, 2) & "', '" _
    36.                  & "Name = '" & .TextMatrix(lcLngi, 3) & "' " _
    37.                  & "WHERE FileNo = '" & .TextMatrix(lcLngi, 0) & "'" '* Replace with your primary key here!
    38.                  
    39.         '* Execute SQL statement using an ADO Connection
    40.         lcObjConn.Execute lcStrSQL
    41.         lcLngErrLevel = 1400
    42.       End If
    43.      
    44.     Next
    45.    
    46.   End With
    47.  
    48.   Set lcObjConn = Nothing
    49.  
    50.   Exit Sub
    51.  
    52. ErrHandler:
    53.  
    54.   If lcLngErrLevel = 1100 Then
    55.     lcFlgUpdate = True
    56.     Resume Next
    57.   Else
    58.     Set lcObjConn = Nothing
    59.     MsgBox Err.Number & ": " & Err.Description
    60.   End If
    61.  
    62. End Sub
    >!v!<
    Free your mind, stop thinking
    http://inspirone.blogspot.com

    Please rate this post if it helped you

  23. #23

    Thread Starter
    Lively Member
    Join Date
    Mar 2002
    Location
    faridabad
    Posts
    101

    to save the data in flexgrid

    hi vbud,

    can u tell me certain things

    1. lcLngErrLevel indicates what?why u have used this field

    2. after these statements it is giving the error

    On Error GoTo ErrHandler
    Set lcObjConn = New ADODB.Connection
    lcStrConnString = "Provider=Micorsoft.Jet.OLEDB.3.51;Data Source=c:\winnt\inventory.mdb;Persist Security Info=False"

    lcObjConn.Open lcStrConnString


    the error is "ado could not find the specific provider"
    can u tell me what is wrong in that

    3. earlier i am having only three fields folder,fileno,name

    but now i add one more field in the starting and gave it the datatype "autonumber" as u have said it is neccesary to have a primary key.but on the form i am not showing this field .

    i set its colwidth property to 0

    now what are the changes i have to made in the code u have given it to me

    thanx in advance

    ishikha
    ishikha

  24. #24
    Hyperactive Member vbud's Avatar
    Join Date
    Jan 2002
    Location
    Mru 20 17S, 57 33E Goal: Get out of the BOX Status: In The Shadows!!! Target Posts: 3,000,000,000
    Posts
    378

    Cool

    1. lcLngErrLevel indicates what?why u have used this field
    Well this is just a variable that will tell me exactly where did the
    error occur.(In our case, its the duplication error although there`s
    a wiser way to do that...).

    2. after these statements it is giving the error

    On Error GoTo ErrHandler
    Set lcObjConn = New ADODB.Connection
    lcStrConnString = "Provider=Micorsoft.Jet.OLEDB.3.51;Data Source=c:\winnt\inventory.mdb;Persist Security Info=False"

    lcObjConn.Open lcStrConnString


    the error is "ado could not find the specific provider"
    can u tell me what is wrong in that
    use this one:
    VB Code:
    1. lcStrConnString = "Provider=Microsoft.Jet.OLEDB.4.0;" _
    2.                   & "Data Source=c:\winnt\inventory.mdb;" _
    3.                   & "User Id=Admin;Password=;"

    its specific for ADO and although you are using Access 97, I
    guess, you can use OLEDB 4.0 instead of 3.51

    try this one:

    VB Code:
    1. Private Sub SaveRecords()
    2.  
    3.   Dim lcLngi        As Long
    4.   Dim lcStrSQL      As String
    5.   Dim lcFlgUpdate   As Boolean
    6.   Dim lcLngErrLevel As Long
    7.   Dim lcObjConn     As ADODB.Connection
    8.   Dim lcStrConnString As String
    9.  
    10.   On Error GoTo ErrHandler
    11.  
    12.   Set lcObjConn = New ADODB.Connection
    13.   lcStrConnString = "Provider=Microsoft.Jet.OLEDB.4.0;" _
    14.                   & "Data Source=c:\winnt\inventory.mdb;" _
    15.                   & "User Id=Admin;Password=;"
    16.  
    17.   lcObjConn.Open lcStrConnString
    18.  
    19.   With MSFlexGrid1
    20.    
    21.     lcLngErrLevel = 1000
    22.    
    23.     For lcLngi = 1 To .Rows '* Or maybe its (Rows - 1)...
    24.          
    25.       lcStrSQL = "INSERT INTO Document(Folder, FileNo, Name) " _
    26.                 & "VALUES('" & .TextMatrix(lcLngi, 1) & "','" _
    27.                 & .TextMatrix(lcLngi, 2) & "','" _
    28.                 & .TextMatrix(lcLngi, 3) & "')"
    29.                      
    30.       lcLngErrLevel = 1100
    31.       '* Execute SQL statement using an ADO Connection
    32.       lcObjConn.Execute lcStrSQL
    33.       lcLngErrLevel = 1200
    34.      
    35.       If lcFlgUpdate Then
    36.         lcLngErrLevel = 1300
    37.         lcStrSQL = "UPDATE Document " _
    38.                  & "SET Folder = '" & .TextMatrix(lcLngi, 1) & "', '" _
    39.                  & "Name = '" & .TextMatrix(lcLngi, 2) & "' " _
    40.          & "FileNo = '" & .TextMatrix(lcLngi, 3) & "'"
    41.                  & "WHERE [insert yout Primary Key Fieldname here] = '" & .TextMatrix(lcLngi, 0) & "'" '* Replace with your primary key here!
    42.                  
    43.         '* Execute SQL statement using an ADO Connection
    44.         lcObjConn.Execute lcStrSQL
    45.         lcLngErrLevel = 1400
    46.       End If
    47.      
    48.     Next
    49.    
    50.   End With
    51.  
    52.   Set lcObjConn = Nothing
    53.  
    54.   Exit Sub
    55.  
    56. ErrHandler:
    57.  
    58.   If lcLngErrLevel = 1100 Then
    59.     lcFlgUpdate = True
    60.     Resume Next
    61.   Else
    62.     Set lcObjConn = Nothing
    63.     MsgBox Err.Number & ": " & Err.Description
    64.   End If
    65.  
    66. End Sub


    Your are most welcomed...let me know how it goes
    >!v!<
    Free your mind, stop thinking
    http://inspirone.blogspot.com

    Please rate this post if it helped you

  25. #25

    Thread Starter
    Lively Member
    Join Date
    Mar 2002
    Location
    faridabad
    Posts
    101

    to save the data in flexgrid

    hi vbud,

    it is giving the syntax error in update statement

    ishikha
    ishikha

  26. #26

    Thread Starter
    Lively Member
    Join Date
    Mar 2002
    Location
    faridabad
    Posts
    101

    to save the data in flexgrid

    hi,

    the code u have given is not working well .it is also adding new record if it is already existing

    ishikha
    ishikha

  27. #27
    Hyperactive Member vbud's Avatar
    Join Date
    Jan 2002
    Location
    Mru 20 17S, 57 33E Goal: Get out of the BOX Status: In The Shadows!!! Target Posts: 3,000,000,000
    Posts
    378
    Sorry for the previous code, i wrote it on the fly and did not even test run it.

    Here it goes:

    VB Code:
    1. Private Sub SaveRecords()
    2.  
    3.   '* 1. When you are inserting new rows inside your grid,
    4.   '*    Make sure that you are moving the primary key too:
    5.   '*    That is move the value in Column 0 just like you
    6.   '*    would do for the other visible columns.
    7.   '* 2. In the Update SQL Statement below, Replace [Key Fieldname] with
    8.   '*    the name of the primary key you are using in your Document table.
    9.   '* 3. As soon as you finish saving your records, IMMEDIATELY reload
    10.   '*    the data from your table back to the grid, to make sure that
    11.   '*    the primary key for the newly inserted records are loaded.
    12.  
    13.   Dim lcLngi          As Long
    14.   Dim lcStrSQL        As String
    15.   Dim lcLngErrLevel   As Long
    16.   Dim lcObjConn       As ADODB.Connection
    17.   Dim lcStrConnString As String
    18.  
    19.   On Error GoTo ErrHandler
    20.  
    21.   Set lcObjConn = New ADODB.Connection
    22.  
    23.   lcStrConnString = "Provider=Microsoft.Jet.OLEDB.4.0;" _
    24.                   & "Data Source=c:\winnt\inventory.mdb;" _
    25.                   & "User Id=Admin;Password=;"
    26.  
    27.   lcObjConn.Open lcStrConnString
    28.  
    29.   With MSFlexGrid1
    30.    
    31.     lcLngErrLevel = 1000
    32.    
    33.     For lcLngi = 1 To .Rows - 1
    34.      
    35.       '* We check if there is a primary key in the first column of the grid
    36.       If Len(Trim$(MSFlexGrid1.TextMatrix(lcLngi, 0))) > 0 Then
    37.         '* if so then Record already exist in database, we update it here
    38.         lcStrSQL = "UPDATE Document " _
    39.                  & "SET Folder = '" & .TextMatrix(lcLngi, 1) & "', " _
    40.                  & "Name = '" & .TextMatrix(lcLngi, 2) & "', " _
    41.                  & "FileNo = '" & .TextMatrix(lcLngi, 3) & "' " _
    42.                  & "WHERE [Key Fieldname] = " & CLng(.TextMatrix(lcLngi, 0)) '* Replace with your primary key here!
    43.      
    44.       Else
    45.         '* Else Insert new record
    46.         lcStrSQL = "INSERT INTO Document(Folder, FileNo, Name) " _
    47.                  & "VALUES('" & .TextMatrix(lcLngi, 1) & "','" _
    48.                  & .TextMatrix(lcLngi, 2) & "','" _
    49.                  & .TextMatrix(lcLngi, 3) & "')"
    50.       End If
    51.  
    52.       '* Execute SQL statement using an ADO Connection
    53.       lcObjConn.Execute lcStrSQL
    54.      
    55.     Next
    56.    
    57.   End With
    58.  
    59.   Set lcObjConn = Nothing
    60.  
    61.   Exit Sub
    62.  
    63. ErrHandler:
    64.  
    65.   Set lcObjConn = Nothing
    66.   MsgBox Err.Number & ": " & Err.Description
    67.  
    68. End Sub
    >!v!<
    Free your mind, stop thinking
    http://inspirone.blogspot.com

    Please rate this post if it helped you

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