Page 1 of 4 1234 LastLast
Results 1 to 40 of 124

Thread: access[Resolved]

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Feb 2005
    Posts
    119

    Resolved access[Resolved]

    How do I Select a table in a database that has been loaded and stuff...I have to take an unknown ammount of fields and put them in a database.
    Last edited by MassImmune; May 10th, 2005 at 11:53 AM.

  2. #2
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: access

    Can you clarify what your trying to do a little more?

    If you have records in a table then wouldnt the fields be known and arent they already in a db?

    Same db or a different one?
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  3. #3

    Thread Starter
    Lively Member
    Join Date
    Feb 2005
    Posts
    119

    Re: access

    No...They are coming from an excel spreadsheet.

  4. #4
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: access

    So your records are in Excel and you need to copy them to an Access table.

    What versions are you running?
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  5. #5

    Thread Starter
    Lively Member
    Join Date
    Feb 2005
    Posts
    119

    Re: access

    It's Office 2000.

  6. #6
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: access

    Ok, lets setup the connection to Access and Excel.

    I am assuming that you do not need to display Access?

    VB Code:
    1. Option Explicit
    2. 'Add a reference to MS ActiveX Data Object 2.x Library
    3. 'Add a reference to MS Excel xx.0 Object Library (9.0 in your case)
    4. Private moCnn As ADODB.Connection
    5. Private moApp As Excel.Application
    6.  
    7. Private Sub Form_Load()
    8.     Set moCnn = New ADODB.Connection
    9.     moCnn.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\RobDog888.mdb;User Id=admin;Password=;"
    10.     moCnn.Open
    11.     Set moApp = New Excel.Application
    12.     moApp.Visible = True
    13. End Sub
    14.  
    15. Private Sub Form_QueryUnload(Cancel As Integer, UnloadMode As Integer)
    16.     If TypeName(moCnn) <> "Nothing" Then
    17.         If moCnn.State = adStateOpen Then moCnn.Close
    18.     End If
    19.     Set moCnn = Nothing
    20.     If TypeName(moApp) <> "Nothing" Then
    21.         moApp.Quit
    22.     End If
    23.     Set moApp = Nothing
    24. End Sub
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  7. #7

    Thread Starter
    Lively Member
    Join Date
    Feb 2005
    Posts
    119

    Re: access

    This is some of what I have so far

    VB Code:
    1. Private Sub cmdBrowse1_Click()
    2.     On Error GoTo MyError
    3.  
    4.     With dlgOpen
    5.         .CancelError = True
    6.         .Flags = cdlOFNFileMustExist Or cdlOFNHideReadOnly Or cdlOFNPathMustExist
    7.         .Filter = "Excel File (*.xls)|*.xls" 'Filters only for txt files
    8.         .ShowOpen 'Open it
    9.         FileName = dlgOpen.FileName  'Assign the filename to the FileName variable.
    10.         txtExcelFile.Text = FileName ' Display the filename in a text box
    11.     End With 'End with
    12.     Exit Sub 'Exit sub
    13. MyError:
    14.     If Err.Number <> cdlCancel Then
    15.         MsgBox Err.Number & " - " & Err.Description
    16.     End If ' End If
    17. End Sub ' End Sub
    18.  
    19. Private Sub cmdBrowse2_Click()
    20.     On Error GoTo MyError
    21.  
    22.     With dlgOpen
    23.         .CancelError = True 'Set the cancelError to True
    24.         .Flags = cdlOFNFileMustExist Or cdlOFNHideReadOnly Or cdlOFNPathMustExist
    25.         .Filter = "Access File (*.mdb)|*.mdb" 'Filters only for txt files
    26.         .ShowOpen 'Open it
    27.         DBFileName = dlgOpen.FileName    'Assign the filename to the FileName variable
    28.         txtAccessFile.Text = DBFileName  'Display the filename in a text box
    29.     End With 'End with
    30.     Exit Sub 'Exit sub
    31. MyError:
    32.     If Err.Number <> cdlCancel Then
    33.         MsgBox Err.Number & " - " & Err.Description
    34.     End If 'End If
    35. End Sub ' End Sub
    36.  
    37. Private Sub cmdLoad_Click(Index As Integer)
    38.  
    39. If FileName = "" Then
    40.     Dim Response As Integer     'Declare Response variable
    41.     Response = MsgBox("Please open an excel file.", vbOKOnly, "Error") 'Dipsplay error msg.
    42. Else
    43.     Dim oWB As Excel.Workbook   'Declare oWB as a new excel workbook
    44.  
    45.     moApp.Visible = True
    46.     Set oWB = moApp.Workbooks.Open(FileName)    'Set oWD as the file name selected previously
    47.     RowCount = ActiveSheet.Cells.SpecialCells(xlCellTypeLastCell).Row 'Get the number of rows
    48.     ColumnCount = ActiveSheet.Cells.SpecialCells(xlCellTypeLastCell).Column 'Get the number of columns
    49.     txtColumns.Text = ColumnCount 'Display the number of columns
    50.     txtrows.Text = RowCount 'Display the number of rows
    51.     frmExceltoOracle.Show
    52.     oWB.Close False     'Set the oWB close to false
    53.     Set oWB = Nothing   'Set the oWb to nothing
    54.     moApp.Visible = False   'Set the visibility of the MoApp to false
    55. End If  'End If
    56. MsgBox "Excel File Loaded Properly" 'Proves that the Database connected properly.
    57. End Sub 'End sub
    58.  
    59.  
    60. Private Sub cmdLoad1_Click()
    61. If DBFileName = "" Then
    62.     Dim Response As Integer     'Declare Response variable
    63.     Response = MsgBox("Please open an Access file.", vbOKOnly, "Error") 'Display error msg
    64. Else
    65.     Dim adoMyConnection As ADODB.Connection     'Declare The ADO connection
    66.  
    67.     Set adoMyConnection = New ADODB.Connection  'Set the adoMyConnection as a new ADO connection
    68.     With adoMyConnection
    69.         .ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & dlgOpen.FileName & ";Persist Security Info=False" 'Set your connection options
    70.         .Open 'Attempt to connect
    71.         MsgBox "Access File loaded Properly" 'Proves that the Database connected properly.
    72.     End With
    73. End If  'End If
    74. End Sub 'End sub
    75.  
    76. Private Sub Form_Load()
    77.     DBFileName = "" 'Set the database file name to nothing
    78.     FileName = ""   'Set the access file name to nothing
    79.     Set moApp = New Excel.Application   'Set moApp as a new excel application
    80. End Sub 'End Sub

  8. #8
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: access

    Just a few pointers to avoid the never terminating excel instance.
    VB Code:
    1. RowCount = ActiveSheet.Cells.SpecialCells(xlCellTypeLastCell).Row 'Get the number of rows
    2. 'Should be:
    3. RowCount = oWB.Sheets(1).Cells.SpecialCells(xlCellTypeLastCell).Row 'Get the number of rows
    4. 'oWB.Sheets(?) where ? is the sheet you need your data from
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  9. #9

    Thread Starter
    Lively Member
    Join Date
    Feb 2005
    Posts
    119

    Re: access

    But now my biggest problem is taking a row from the excel spreadsheet and putting it into the database. Is it even possible the way I'm doing it by reading each field in the spreadsheet at a time.

  10. #10
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: access

    Do you have allot of rows in Excel? If so then it would be better to use ADO to connect to the sheet
    and insert it into access.
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  11. #11

    Thread Starter
    Lively Member
    Join Date
    Feb 2005
    Posts
    119

    Re: access

    The number of rows will never be determined...It depends on the set up that the client has going.

  12. #12
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: access

    Ok, I'll post an example of connecting to Excel using ADO. After you query the sheet all you do is loop
    through the recordset executing an insert statement of the current record. The only problem will be to alter
    the table to match the sheet on the fly.
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  13. #13
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: access

    Here is a sample I wrote that will get you going. All you need to do is the dynamic table alterations
    depending on excels used columns.

    VB Code:
    1. Option Explicit
    2. 'Add a reference to MS ActiveX Data Objects 2.x Library
    3. 'Add a command button (Command1)
    4. Private moCnn As ADODB.Connection
    5.  
    6. Private Sub Command1_Click()
    7.  
    8.     Dim cnnXls As ADODB.Connection
    9.     Dim rsSchema As ADODB.Recordset
    10.     Dim rsXl As ADODB.Recordset
    11.     Dim lRecs As Long
    12.     Dim iCount As Integer
    13.     Dim iCols As Integer
    14.     Dim i As Integer
    15.    
    16.     Set cnnXls = New ADODB.Connection
    17.     cnnXls.Open "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\Book1.xls;Extended Properties=Excel 8.0;"
    18.     Set rsSchema = cnnXls.OpenSchema(adSchemaTables)
    19.     Set rsXl = New ADODB.Recordset
    20.     rsXl.Open "SELECT * FROM '" & rsSchema.Fields("TABLE_NAME").Value & "'", cnnXls, adOpenStatic, adLockReadOnly, adCmdText
    21.     rsSchema.Close
    22.     Set rsSchema = Nothing
    23.    
    24.     iCount = rsXl.RecordCount
    25.     iCols = rsXl.Fields.Count
    26.    
    27.     If rsXl.BOF = True And rsXl.EOF = True Then
    28.         MsgBox "No Excel data found"
    29.     Else
    30.         For i = 1 To rsXl.RecordCount
    31.             moCnn.Execute "INSERT INTO Table1 VALUES ('" & rsXl.Fields(0).Value & "', '" & rsXl.Fields(1).Value & "')", lRecs
    32.             rsXl.MoveNext
    33.         Next
    34.     End If
    35.    
    36.     If rsXl.State = adStateOpen Then rsXl.Close
    37.     Set rsXl = Nothing
    38.     If cnnXls.State = adStateOpen Then cnnXls.Close
    39.     Set cnnXls = Nothing
    40.    
    41. End Sub
    42.  
    43. Private Sub Form_Load()
    44.     Set moCnn = New ADODB.Connection
    45.     moCnn.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\RobDog888.mdb;User Id=admin;Password=;"
    46.     moCnn.Open
    47. End Sub
    48.  
    49. Private Sub Form_QueryUnload(Cancel As Integer, UnloadMode As Integer)
    50.     If TypeName(moCnn) <> "Nothing" Then
    51.         If moCnn.State = adStateOpen Then moCnn.Close
    52.     End If
    53.     Set moCnn = Nothing
    54. End Sub
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  14. #14

    Thread Starter
    Lively Member
    Join Date
    Feb 2005
    Posts
    119

    Re: access

    It's probably asking to much but is there a way that you can break it up into sections so I know exactly whats going on and what each bit of code is doing...I'm just a noobie remember...

  15. #15
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: access

    No problem at all. Actually it nice to hear that someone actually want to understand and learn from the
    code instead of the usual copy and paste code and poof - I'm a programmer.

    VB Code:
    1. 'This first line set a requirement that all variables are declared
    2. Option Explicit
    3. 'Then we add a reference to ADO (for early binding)
    4. 'Add a reference to MS ActiveX Data Objects 2.x Library
    5. 'Add a command button (Command1)
    6. 'Decalre a module level var connection object to use to connect to Access.
    7. Private moCnn As ADODB.Connection
    8.  
    9. Private Sub Command1_Click()
    10.     'Declare a connection object to connect to excel sheet.
    11.     Dim cnnXls As ADODB.Connection
    12.     'Declare a recordset object to get the structure of the workbook- sheet names, etc
    13.     Dim rsSchema As ADODB.Recordset
    14.     'Declare a rs for reading the data off of the sheet
    15.     Dim rsXl As ADODB.Recordset
    16.     Dim lRecs As Long
    17.     Dim iCount As Integer
    18.     Dim iCols As Integer
    19.     Dim i As Integer
    20.    
    21.     'start a new instance of the excel connection
    22.     Set cnnXls = New ADODB.Connection
    23.     'Open the connection
    24.     cnnXls.Open "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\Book1.xls;Extended Properties=Excel 8.0;"
    25.     'set a rs to get the workbook structure.
    26.     Set rsSchema = cnnXls.OpenSchema(adSchemaTables)
    27.     'set a new rs that will read the excel data
    28.     Set rsXl = New ADODB.Recordset
    29.     'open/retrieve data on the sheet passed in rsSchema (first sheet since we are not searching for a specific sheet.
    30.     'This can be added if its a specific sheet thats not the first one).
    31.     'rsSchema.Fields("TABLE_NAME").Value = "Sheet1" - first sheet or whatever the first sheet it named.
    32.     rsXl.Open "SELECT * FROM '" & rsSchema.Fields("TABLE_NAME").Value & "'", cnnXls, adOpenStatic, adLockReadOnly, adCmdText
    33.     'clean up
    34.     rsSchema.Close
    35.     Set rsSchema = Nothing
    36.    
    37.     iCount = rsXl.RecordCount
    38.     iCols = rsXl.Fields.Count
    39.     'test for returned data in the rs
    40.     If rsXl.BOF = True And rsXl.EOF = True Then
    41.         MsgBox "No Excel data found"
    42.     Else
    43.         'Read the data that is in the first sheet and add it to Access by way of
    44.         'the recordset set to the Access table - Table1, one record at a time
    45.         For i = 1 To rsXl.RecordCount
    46.             moCnn.Execute "INSERT INTO Table1 VALUES ('" & rsXl.Fields(0).Value & "', '" & rsXl.Fields(1).Value & "')", lRecs
    47.             rsXl.MoveNext
    48.         Next
    49.     End If
    50.     'clean up the recordset and connection
    51.     If rsXl.State = adStateOpen Then rsXl.Close
    52.     Set rsXl = Nothing
    53.     If cnnXls.State = adStateOpen Then cnnXls.Close
    54.     Set cnnXls = Nothing
    55.    
    56. End Sub
    57.  
    58. Private Sub Form_Load()
    59.     'start a new connection to Access
    60.     Set moCnn = New ADODB.Connection
    61.     moCnn.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\RobDog888.mdb;User Id=admin;Password=;"
    62.     moCnn.Open
    63. End Sub
    64.  
    65. Private Sub Form_QueryUnload(Cancel As Integer, UnloadMode As Integer)
    66.     'clean up/close the connection to access
    67.     If TypeName(moCnn) <> "Nothing" Then
    68.         If moCnn.State = adStateOpen Then moCnn.Close
    69.     End If
    70.     Set moCnn = Nothing
    71. End Sub
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  16. #16

    Thread Starter
    Lively Member
    Join Date
    Feb 2005
    Posts
    119

    Re: access

    Quote Originally Posted by RobDog888

    rsXl.Open "SELECT * FROM '" & rsSchema.Fields("TABLE_NAME").Value & "'", cnnXls, adOpenStatic, adLockReadOnly, adCmdText
    Everytime I run this line it comes back with an error that says: Syntax error in query. Incomplete query clause.

    I'm not sure what it means or if I'm supposed to put something in there or not.

  17. #17
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: access

    After the rsSchema is opened add this line and place a breakpoint on it.
    VB Code:
    1. Debug.Print rsSchema.Fields("TABLE_NAME").Value
    Whats the value?
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  18. #18

    Thread Starter
    Lively Member
    Join Date
    Feb 2005
    Posts
    119

    Re: access

    Nothing happened...unless I put it in the wrong spot...and I'm not sure what you mean by values.

  19. #19
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: access

    Before this line:
    VB Code:
    1. rsXl.Open "SELECT * FROM '" & rsSchema.Fields("TABLE_NAME").Value & "'", cnnXls, adOpenStatic, adLockReadOnly, adCmdText
    Place the debug.print code I previously posted and view the results in the immediate window.

    So it should look like this...

    VB Code:
    1. Debug.Print rsSchema.Fields("TABLE_NAME").Value
    2. rsXl.Open "SELECT * FROM '" & rsSchema.Fields("TABLE_NAME").Value & "'", cnnXls, adOpenStatic, adLockReadOnly, adCmdText
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  20. #20

    Thread Starter
    Lively Member
    Join Date
    Feb 2005
    Posts
    119

    Re: access

    It still came up with the same error as before.

  21. #21
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: access

    But what does this display in the immediate window. This is important to determine what the error is
    comming from. Also try stepping through the code to see what line.

    VB Code:
    1. Debug.Print rsSchema.Fields("TABLE_NAME").Value
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  22. #22

    Thread Starter
    Lively Member
    Join Date
    Feb 2005
    Posts
    119

    Re: access

    Is this what you mean?


  23. #23
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: access

    So Sheet1$ is your sheet name being retrieved by the rsSchema recordset.
    The error may be coming from the $ sign. Let me test something.
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  24. #24
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: access

    Ok, found the issue. Seems the sheet name needs to be wrapped around with the "`" character. Its the key
    just to the left of the #1 key and above the Tab key. "`" = ASCII 96.

    VB Code:
    1. Private Sub Command1_Click()
    2.     'Declare a connection object to connect to excel sheet.
    3.     Dim cnnXls As ADODB.Connection
    4.     'Declare a recordset object to get the structure of the workbook- sheet names, etc
    5.     Dim rsSchema As ADODB.Recordset
    6.     'Declare a rs for reading the data off of the sheet
    7.     Dim rsXl As ADODB.Recordset
    8.     Dim lRecs As Long
    9.     Dim iCount As Integer
    10.     Dim iCols As Integer
    11.     Dim i As Integer
    12.    
    13.     'start a new instance of the excel connection
    14.     Set cnnXls = New ADODB.Connection
    15.     'Open the connection
    16.     cnnXls.Open "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\Book1.xls;Extended Properties=Excel 8.0;"
    17.     'set a rs to get the workbook structure.
    18.     Set rsSchema = cnnXls.OpenSchema(adSchemaTables)
    19.     'set a new rs that will read the excel data
    20.     Set rsXl = New ADODB.Recordset
    21.     'open/retrieve data on the sheet passed in rsSchema (first sheet since we are not searching for a specific sheet.
    22.     'This can be added if its a specific sheet thats not the first one).
    23.     'rsSchema.Fields("TABLE_NAME").Value = "Sheet1" - first sheet or whatever the first sheet it named.
    24.     Debug.Print rsSchema.Fields("TABLE_NAME").Value
    25.     'Filter the rs so its on the record (sheet name) we need.
    26.     rsSchema.Filter = "TABLE_NAME = 'Sheet1$'" '<-- CHANGE TO THE DESIRED SHEET NAME
    27.     If rsSchema.BOF = True And rsSchema.EOF = True Then
    28.         MsgBox "Excel sheet not found!", vbOKOnly + vbExclamation
    29.         GoTo CleanUp
    30.     End If
    31.     rsXl.Open "SELECT * FROM `" & rsSchema.Fields("TABLE_NAME").Value & "`", cnnXls, adOpenStatic, adLockReadOnly, adCmdText
    32.     'clean up
    33.     rsSchema.Close
    34.     Set rsSchema = Nothing
    35.    
    36.     iCount = rsXl.RecordCount
    37.     iCols = rsXl.Fields.Count
    38.     'test for returned data in the rs
    39.     If rsXl.BOF = True And rsXl.EOF = True Then
    40.         MsgBox "No Excel data found"
    41.     Else
    42.         'Read the data that is in the first sheet and add it to Access by way of
    43.         'the recordset set to the Access table - Table1, one record at a time
    44.         For i = 1 To rsXl.RecordCount
    45.             moCnn.Execute "INSERT INTO Table1 VALUES ('" & rsXl.Fields(0).Value & "', '" & rsXl.Fields(1).Value & "')", lRecs
    46.             rsXl.MoveNext
    47.         Next
    48.     End If
    49. CleanUp:
    50.     'clean up the recordset and connection
    51.     If rsXl.State = adStateOpen Then rsXl.Close
    52.     Set rsXl = Nothing
    53.     If cnnXls.State = adStateOpen Then cnnXls.Close
    54.     Set cnnXls = Nothing
    55. End Sub
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  25. #25

    Thread Starter
    Lively Member
    Join Date
    Feb 2005
    Posts
    119

    Re: access

    That solved that problem......but now it says that the number of query values and destination fields are not the same...

  26. #26
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: access

    So then that means that you number of columns in Excel does not match the number of feilds in your Access table.
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  27. #27

    Thread Starter
    Lively Member
    Join Date
    Feb 2005
    Posts
    119

    Re: access

    I noticed I had the wrong excel file selected but when I use the new one it still says that the number of query values and destination fields are not the same.
    Last edited by MassImmune; Apr 11th, 2005 at 08:01 AM.

  28. #28
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: access

    How many fields in your access table are there and how many used columns (max) are there in your
    sheet? They need to match.
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  29. #29

    Thread Starter
    Lively Member
    Join Date
    Feb 2005
    Posts
    119

    Re: access

    I have 4 fields in the database and I have 4 columns in excel...So I don't see why it won't work for me.

  30. #30
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: access

    Step through the code and check the value for rsXl.Fields.Count to make sure it is getting the correct column count.
    Then make sure your field types match the data types in your column in Excel.
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  31. #31

    Thread Starter
    Lively Member
    Join Date
    Feb 2005
    Posts
    119

    Re: access

    Yeah...After both the iColumn and iCols count lines I added a messege box that shows the value of the 2 variables and they both say 4.

  32. #32
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: access

    iColumn ? Could you post your updated code?
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  33. #33

    Thread Starter
    Lively Member
    Join Date
    Feb 2005
    Posts
    119

    Re: access

    Typo...I meant to put iCount...sorry about that.

  34. #34
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: access

    Oh, iCount is the record or row count. Not the field count.
    iCols = rsXl.Fields.Count is the column count in Excel.
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  35. #35

    Thread Starter
    Lively Member
    Join Date
    Feb 2005
    Posts
    119

    Re: access

    Are those to values supposed to be equal...After I run it and the error comes up I click debug and it highlights this line so maybe it's something wrong with it:
    VB Code:
    1. moCnn.Execute "INSERT INTO Table1 VALUES ('" & rsXl.Fields(0).Value & "', '" & rsXl.Fields(1).Value & "')", lRecs

  36. #36
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: access

    Ok, in Access, your table consists of what fields? Also that line needs to be modified to match your number
    of fields and column.

    VB Code:
    1. rsXl.Fields(0).Value, 1, 2, ... etc matching the column in excel and the feilds in access
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  37. #37

    Thread Starter
    Lively Member
    Join Date
    Feb 2005
    Posts
    119

    Re: access

    The 4 fields are Name, Age, Birthdate(just the year) and income...

  38. #38
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: access

    Replace the line with this.
    VB Code:
    1. moCnn.Execute "INSERT INTO Table1 VALUES ('" & rsXl.Fields(0).Value & "', '" & rsXl.Fields(1).Value & "', '" & rsXl.Fields(2).Value & "', '" & rsXl.Fields(3).Value & "')", lRecs
    What you also need to watch the field data types. If you have a field as Numeric then you dont need the single quotes are around that particular field, only text fields.
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  39. #39

    Thread Starter
    Lively Member
    Join Date
    Feb 2005
    Posts
    119

    Re: access

    For some reason it isn't taking the first row in the excel file. It works but when I check the database, The first row from the excel file is missing.

  40. #40
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: access

    How about doing a rsXl.MoveFirst befor the insert loop?
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

Page 1 of 4 1234 LastLast

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