Results 1 to 2 of 2

Still Learning VBA.... My SQL seems off

Threaded View

  1. #1
    Registered User
    Join Date
    12-26-2018
    Location
    Chicago, IL
    MS-Off Ver
    2016
    Posts
    1

    Post Still Learning VBA.... My SQL seems off

    Hello Guys ans Gals,

    I'm new to writing VBA code so please excuse my ignorance. I have been using the same bit of code to extract data from an Access database (three seperate databases). This time, for some reason, it is not working. See below...
    Option Explicit
    
    Const QA As String = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=\\cherubim\Production Database\Production Database.accdb; Persist Security Info=False;"
    Const MiniMRP5 As String = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=\\cherubim\MRP\MiniMRP5\data\MRP5Data.accdb; Persist Security Info=False;"
    Const MiniMRP4 As String = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=\\cherubim\RW Shares\MiniMRP DataMine\MiniMRP Data Mine.accdb; Persist Security Info=False;"
    
    
    Private Sub Workbook_Open()
    
        Dim Conn As Object
        Dim Data As Object
        Dim Field As Object
            
        Set Conn = CreateObject("ADODB.Connection")
        Set Data = CreateObject("ADODB.Recordset")
        
        Conn.ConnectionString = MiniMRP5
        Conn.Open
        
        On Error GoTo CloseConnection
        
       
       With Data
            .ActiveConnection = Conn
            .Source = getSQLString
            .LockType = 1
            .CursorType = 0
            .Open
            End With
            
        On Error GoTo CloseRecordset
        
          Worksheets.Add
          For Each Field In Data.Fields
            ActiveCell.Value = Field.Name
            ActiveCell.Offset(0, 1).Select
            
            Next Field
            
          Range("A1").Select
          Range("A2").CopyFromRecordset Data
          Range("A1").CurrentRegion.EntireColumn.AutoFit
      
      On Error GoTo 0
              
    CloseRecordset:
         Data.Close
         
    CloseConnection:
         Conn.Close
       
        End Sub
    
    Function getSQLString() As String
    Dim JOB As String
    Dim SQLstring As String
    
    JOB = Application.InputBox("Enter JOB #")
    
    SQLstring = "SELECT tblworders.SWONo, tblworders.WOQty, tblcustorderdetail.COID, tblstockitems.MasterPNo, tblstockitems.Rev, tblstockitems.ItemDescription, tblstockitems.Cust1, tblstockitems.[OP 10], tblstockitems.[OP 20], tblstockitems.[OP 30], tblstockitems.[OP 40], tblstockitems.[OP 50], tblstockitems.[OP 60], tblstockitems.[OP 70], tblstockitems.[OP 80], tblstockitems.[OP 90], tblstockitems.[OP 100], tblstockitems.Cust3 " & _
    "FROM tblworders INNER JOIN (tblcustorderdetail INNER JOIN tblstockitems ON tblcustorderdetail.StockID = tblstockitems.ItemID) ON tblworders.CustORID = tblcustorderdetail.COID " & _
    "WHERE (tblworders.SWONo) = ""[JOB]"""
    
    getSQLString = SQLstring
    
    End Function
    In this instance, I extract the field headers but none of the actual data stored in the tables is being pulled into Excel. I have a feeling there is some error in my SQL. Can anyone help?
    Last edited by Leith Ross; 12-26-2018 at 04:47 PM. Reason: Added Code Tags

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Replies: 8
    Last Post: 02-12-2020, 02:47 AM
  2. Learning
    By ShashiSuppi in forum The Water Cooler
    Replies: 2
    Last Post: 08-01-2017, 05:54 AM
  3. Hi All Just learning here.
    By Windyz in forum Hello..Introduce yourself
    Replies: 1
    Last Post: 12-01-2016, 01:38 AM
  4. Learning...
    By kand526 in forum Hello..Introduce yourself
    Replies: 1
    Last Post: 02-05-2014, 10:57 PM
  5. Still learning .....
    By Weatherbee50 in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 05-17-2012, 08:49 PM
  6. Learning VBA - Suggestion for Beginners Learning Curve
    By sighlent1 in forum Excel General
    Replies: 1
    Last Post: 08-26-2010, 12:58 PM
  7. Learning VBA
    By motomoto1 in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 03-29-2010, 11:13 AM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1