Results 1 to 20 of 20

Thread: [RESOLVED] Export flexgrid to SQL

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Mar 2006
    Posts
    245

    Resolved [RESOLVED] Export flexgrid to SQL

    Hi all.
    I have been messing around with Excel and VB. Now I have the Excel imported over to a flexgrid. It took alot of frustration for something simple but with help from si_the_geek, we were able to make it all happen. Thanks mucho. Now I was wondering if it was possible to take this information and send it to a SQL Server database table. What would be the best way to approach it? Or if possible to take it directly from Excel to the database. But I prefer to have the flexgrid just so i can see everything in VB before it goes. I have no code yet nor really asking for code at this point. Just a kick in the right direction or a tutorial someone may have laying around would be great for now. thanks alot.

    tibor

  2. #2
    PowerPoster
    Join Date
    Feb 2006
    Location
    East of NYC, USA
    Posts
    5,691

    Re: Export flexgrid to SQL

    Read the data into an array, populate the Flexgrid and fill the database table from the array (I do it this way in a couple of programs) or read the Flexgrid and send the data to the table.

    Either way is valid.
    The most difficult part of developing a program is understanding the problem.
    The second most difficult part is deciding how you're going to solve the problem.
    Actually writing the program (translating your solution into some computer language) is the easiest part.

    Please indent your code and use [HIGHLIGHT="VB"] [/HIGHLIGHT] tags around it to make it easier to read.

    Please Help Us To Save Ana

  3. #3
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974

    Re: Export flexgrid to SQL

    (for those of you who don't know it, the code used to fill a Flexgrid is here).

    Al42, I'm afraid I dont see the purpose of an array as you described - it seems like a wasted step (but I may have read it wrong!).


    Anyway, back to the question.. The main problem you have is in identifying the rows in the grid that have been edited (or added/deleted), and refering them back to the rows in the database.

    To do this you will need to keep track somehow of the rows edited/added/removed. Presumably you will be using code at each point of the process (as the Flexgrid does not allow editing by default), so you can store the relevant information - possibly in arrays (or collections, or ...). When you want to update the database, refer to these to find out which rows you need to work with.

    To ensure you are working with the right rows in the database, I would recommend changing the "fill" code, so that it also sets the RowData property for each row - using a unique (numerical) field in the database table. When you are doing the actual writing to the database, retrieve this value and use it as appropriate in Update/Insert/Delete SQL statements.

  4. #4

    Thread Starter
    Addicted Member
    Join Date
    Mar 2006
    Posts
    245

    Re: Export flexgrid to SQL

    could i do it this way. with the flexgrid, just shut everything down that used excel objects and just use the flexgrid to show the data. then just use excel as a database object and run sql statements to insert and update?

  5. #5

    Thread Starter
    Addicted Member
    Join Date
    Mar 2006
    Posts
    245

    Re: Export flexgrid to SQL

    And on a side note I am having a problem with there still being an instance of Excel running even after i set these

    Set oXLSheet = Nothing
    Set oXLBook = Nothing
    Set oXLApp = Nothing

    How would i go about ridding this after everytime i run my program?

  6. #6
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974

    Re: Export flexgrid to SQL

    Setting the objects to Nothing doesn't close Excel - it just removes your link (which you need to do too). See my Excel Tutorial for the method to close Excel (post #5), and some useful tips on making sure that Excel closes properly (post 11).
    Quote Originally Posted by tibor
    could i do it this way. with the flexgrid, just shut everything down that used excel objects and just use the flexgrid to show the data. then just use excel as a database object and run sql statements to insert and update?
    Is Excel your current "database"? If so, that is basically what I was suggesting.

  7. #7

    Thread Starter
    Addicted Member
    Join Date
    Mar 2006
    Posts
    245

    Re: Export flexgrid to SQL

    Ok sorry i misread your post. I think that is the approach i will try. Results to come...

  8. #8

    Thread Starter
    Addicted Member
    Join Date
    Mar 2006
    Posts
    245

    Re: Export flexgrid to SQL

    Wow cant believe i missed that in the tutuorial. We are getting closer now, lol

  9. #9

    Thread Starter
    Addicted Member
    Join Date
    Mar 2006
    Posts
    245

    Re: Export flexgrid to SQL

    Ok ive ran into a bit of a problem. apparently my tables are going to be too big for a flexgrid. so i tried setting oXLApp.Visible = True so i can have the spreadsheet open instead of a flexgrid but it is telling me object required.

  10. #10
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974

    Re: Export flexgrid to SQL

    apparently my tables are going to be too big for a flexgrid
    That's news to me.. what is the problem/error here?

    i tried setting oXLApp.Visible = True so i can have the spreadsheet open instead of a flexgrid but it is telling me object required
    Presumably you have the scope wrong - you have declared oXLApp is a different sub to where you are using it.

  11. #11

    Thread Starter
    Addicted Member
    Join Date
    Mar 2006
    Posts
    245

    Re: Export flexgrid to SQL

    it gives me a error of

    unable to allocate memory for flexgrid

    so i assumed it meant that it was too much data

    i have oXPApp declared as an object

  12. #12
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974

    Re: Export flexgrid to SQL

    Hmmm.. it sounds like you are right.
    Quote Originally Posted by tibor
    i have oXPApp declared as an object
    The important thing is where is it declared, and where are you trying to use it?

    Presumably the declaration (and inital use) is in one sub, and this code is in another.. am I right?

  13. #13

    Thread Starter
    Addicted Member
    Join Date
    Mar 2006
    Posts
    245

    Re: Export flexgrid to SQL

    it is declared as a public in a module. it is being used on in 2 possible areas depending on what is selected. but both selections lead to the same code, just one is being pulled from SQL to Excel then at one time to the flexgrid. and the other is just any Excel file.

  14. #14
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974

    Re: Export flexgrid to SQL

    The "object required" error is telling you that the object variable (oXLApp) is not valid at that point - the usual cause (on the forums at least) is that the object variable is not declared in the relevant scope, or has been re-declared in a second scope (eg: within the sub, when it is also declared in a module).

    The other cause is that the code is being run before/after the object is valid (eg: before the first "Set oXLApp" line, or after the "oXLApp.Close")

  15. #15

    Thread Starter
    Addicted Member
    Join Date
    Mar 2006
    Posts
    245

    Re: Export flexgrid to SQL

    hmm thats just wierd i sifted through the code and everything seemed to be fine and i tried placing that statement in different areas and still got the error.

  16. #16
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974

    Re: Export flexgrid to SQL

    In that case you'd better upload the files, so that I can find out what the problem is.

  17. #17

    Thread Starter
    Addicted Member
    Join Date
    Mar 2006
    Posts
    245

    Re: Export flexgrid to SQL

    ok heres the code in its entirety


    Main Page
    VB Code:
    1. Option Explicit
    2.  
    3. Private Sub cmdLoad_Click()
    4. Set oXLApp = CreateObject("Excel.Application")
    5. Set oXLBook = oXLApp.workbooks.Open(Dir1.Path & "\" & (File1))
    6. Set oXLSheet = oXLBook.Worksheets(1)
    7.  
    8. vArray = oXLSheet.UsedRange.Value
    9.  
    10.   Set oXLSheet = Nothing
    11.   Set oXLBook = Nothing
    12.  
    13.  
    14. ExceltoSQLSub.Show
    15. Unload Me
    16. End Sub
    17.  
    18.  
    19.  
    20. Private Sub cmdMasLoad_Click()
    21.  
    22. Dim SQL As String
    23. Dim Rs As New Recordset
    24. cmdMasLoad.Enabled = False
    25. cmdMasLoad.Caption = "Wait"
    26. Call Connect2
    27.  
    28.          SQL = "SELECT * FROM tpa00175 "
    29.          Set Rs = New Recordset
    30.          Rs.Open SQL, oConn, adOpenDynamic, adLockOptimistic
    31.          Set oXLApp = CreateObject("Excel.Application")
    32.          Set oXLApp.Visible = True
    33.          Set oXLBook = oXLApp.workbooks.Open(App.Path & "\Project.xls")
    34.          Set oXLSheet = oXLBook.Worksheets(1)
    35.          oXLBook.Worksheets(1).range("A2").CopyFromRecordset Rs
    36.        
    37.  
    38. vArray = oXLSheet.UsedRange.Value
    39.  
    40.   Set oXLSheet = Nothing
    41.   oXLBook.Save
    42.          oXLBook.Close False
    43.          
    44.          Set oXLBook = Nothing
    45.          Set oXLApp = Nothing
    46.  
    47. cmdMasLoad.Enabled = True
    48. cmdMasLoad.Caption = "Load"
    49. ExceltoSQLSub.Show
    50. Unload Me
    51. End Sub
    52.  
    53. Private Sub Dir1_Change()
    54. File1.FileName = Dir1.Path
    55. End Sub
    56.  
    57. Private Sub Drive1_Change()
    58. Dir1.Path = Drive1.Drive
    59.  
    60. End Sub
    61.  
    62. Private Sub File1_Click()
    63. cmdLoad.Enabled = True
    64. End Sub
    65.  
    66. Private Sub File1_DblClick()
    67. cmdLoad_Click
    68. End Sub
    69.  
    70. Private Sub Form_Load()
    71. lstTypes.AddItem "Excel File (.XLS)"
    72. lstTypes.ListIndex = 0
    73. If lstTypes.ListIndex = 0 Then
    74. File1.Pattern = "*.XLS"
    75. Else
    76. End If
    77. Dir1.Path = "C:\"
    78. cmdLoad.Enabled = False
    79. End Sub

    Flexgrid Page

    VB Code:
    1. Option Explicit
    2.  
    3. Private Sub cmdImport_Click()
    4. Dim strsql As String
    5. Dim ors As Recordset
    6.  
    7. Call Connect
    8. strsql = "Insert into MyTest Select * FROM OPENROWSET('Microsoft.Jet.OLEDB.4.0',"
    9. strsql = strsql & "'Excel 8.0;Database=C:\MyFile.xls;HDR=YES', 'SELECT * FROM [Sheet1$]')"
    10. Debug.Print CStr(strsql)
    11. Set ors = New Recordset
    12. ors.Open strsql, oConn, adOpenDynamic, adLockOptimistic
    13. Unload Me
    14. frmExceltoSQLMain.Show
    15. End Sub
    16.  
    17. Private Sub Form_Load()
    18.  
    19.  
    20. Dim lngRow As Long, lngCol As Long
    21.   With MSFlexGrid1
    22.     .FixedRows = 1
    23.     .FixedCols = 0
    24.     .Cols = UBound(vArray, 2)   'set the number of rows/cols
    25.     .Rows = UBound(vArray, 1)
    26.     For lngRow = 0 To .Rows - 1 'fill the text
    27.       For lngCol = 0 To .Cols - 1
    28.         .TextMatrix(lngRow, lngCol) = vArray(lngRow + 1, lngCol + 1)
    29.       Next lngCol
    30.     Next lngRow
    31.   End With
    32.  
    33.  
    34.  
    35.   Set vArray = Nothing
    36.   oXLApp.Quit
    37.   Set oXLApp = Nothing
    38.  
    39. End Sub
    40. Private Sub cmdClose_Click()
    41. oConn.Close
    42. Unload Me
    43. frmMain.Show
    44.  
    45. End Sub

    and the module

    VB Code:
    1. Option Explicit
    2. Public oXLBook As Object
    3. Public oXLSheet As Object
    4. Public my_variable As String
    5. Public oXLApp As Object
    6. Public vArray As Variant
    7. Public lngCol As Long, lngRow As Long
    8. Public oConn As New Connection
    9.  
    10. Public Sub Connect()
    11.  
    12.  
    13. oConn.Properties("Prompt") = adPromptAlways
    14. oConn.Open "Driver={SQL Server};Server=(local);DataBase=pubs;password=password;User ID=sa"
    15.  
    16. End Sub
    17.  
    18. Public Sub Connect2()
    19. oConn.Properties("Prompt") = adPromptAlways
    20. oConn.Open "Driver={SQL Server};Server=(local);DataBase=mas500_app;password=password;User ID=sa"
    21.  
    22. End Sub

  18. #18
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974

    Re: Export flexgrid to SQL

    In "main page":
    you don't close the objects in cmdLoad_Click.

    In "Flexgrid Page":
    you dont need a recordset in cmdImport_Click, just use oConn.Execute strsql
    in Form_Load, you should not be refering to oXLApp - anything to do with it should be in the place where it is used (presumably cmdLoad_Click in "main page").


    The only "Set oXLApp.Visible = True" I can see is fine, but may need a DoEvents before it.

  19. #19

    Thread Starter
    Addicted Member
    Join Date
    Mar 2006
    Posts
    245

    Re: Export flexgrid to SQL

    Ok thanks alot for the input. im about to call it a day and will get back to it first thing in the morning and let ya know how it comes about.

  20. #20

    Thread Starter
    Addicted Member
    Join Date
    Mar 2006
    Posts
    245

    Re: Export flexgrid to SQL

    Ok i found out why i was getting this error. i was writing it like

    Set oXLApp.Visible = True

    when it should have been

    oXLApp.Visible=True

    Thats one problem down, lol.

    tibor

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