Results 1 to 2 of 2

Create Table; Update Table: MS Query

Threaded View

Guest Create Table; Update Table:... 10-07-2008, 10:31 AM
Guest The answer is the script has... 10-08-2008, 06:39 AM
  1. #1
    astrodon
    Guest

    Create Table; Update Table: MS Query

    I have created an addin that among other things, has the ability to open *.sql files, read the contents of those files into a string variable; then that variable used in MS Query to populate a spreadsheet. It works swimmingly for any select query I have fed to it but action queries return an error. The question is are action queries off limits to MS Query? I will show first the VB I am using. The typical script is too long to post so have attached a text file of a typical script for my purposes.

    Public Sub ReadAsciiFile()
    
        Dim szFileName As String
        Dim iFileNum As Integer
        Dim sBuf As String
        Dim SQL As String
        Dim szCatalog As String
        
        szCatalog = InputBox("SELECT [ ASCEND = 'A' :: PROVAL = 'P' ]", "SELECT DATABASE", "P")
        Select Case UCase(szCatalog)
            Case "A"
                szCatalog = "Ascend"
            Case Else
                szCatalog = "Proval"
        End Select
        
    '   open common dialog form and get filename
        UserForm1.Show
        szFileName = g_ScriptFile
    
        ' does the file exist?  simpleminded test:
        If Len(Dir$(szFileName)) = 0 Or Len(szFileName) = 0 Then
            Exit Sub
        End If
    
        iFileNum = FreeFile()
        Open szFileName For Input As iFileNum
    
        Do While Not EOF(iFileNum)
            Line Input #iFileNum, sBuf
            
        '   we want to be able to have notes in the script
            If InStr(sBuf, "--") = 0 Then
                SQL = SQL & sBuf & vbCrLf
            End If
        Loop
        Debug.Print SQL
        ' close the file
        Close iFileNum
        
        If Not IsOpen(SQL, szCatalog, True) Then
            Call BadDataError("ReadAsciiFile")
        End If
    
    End Sub
    
    '    the 'SQL' is then used in the following
    
    Public Function IsOpen(ByVal szSql As String, _
                           Optional ByVal szDatabase As String = "Proval", _
                           Optional ByVal fUseHeader As Boolean = False) As Boolean
    Dim fOpen As Boolean
    Dim szDSN As String
    Dim szCatalog As String
    Dim szUser As String
    Dim szPswd As String
    On Error GoTo EH
    
    '   read connection parameters from READER sheet range names
        fOpen = True            '   assume the best
        szDSN = GetDSN          '   dsn name
        Call ClearNames         '   clear previous query names
        szDatabase = UCase(szDatabase)
        
        Select Case szDatabase
            Case "PROVAL"
                szCatalog = GetProvalDB
                szUser = GetProvalUser
                szPswd = GetProvalPass
            Case Else
                szCatalog = GetAscendDb
                szUser = GetAscendUser
                szPswd = GetAscendPass
        End Select
        
        With ActiveSheet.QueryTables.Add(Connection:= _
            "ODBC;DSN=" & szDSN & ";" & "UID=" & szUser & ";" & _
            "PWD=" & szPswd & ";DATABASE=" & szCatalog, Destination:=Range(ActiveCell.Address))
            .CommandText = szSql
            .Name = "QRY_" & Format(Now(), "mmddyy_hhmm")   '   create query name
            .FieldNames = fUseHeader
            .RowNumbers = False
            .FillAdjacentFormulas = True
            .PreserveFormatting = True
            .RefreshOnFileOpen = False
            .BackgroundQuery = True
            .RefreshStyle = xlOverwriteCells
            .SavePassword = True
            .SaveData = True
            .AdjustColumnWidth = False
            .RefreshPeriod = 0
            .PreserveColumnInfo = True
            .Refresh BackgroundQuery:=False
        End With
        
    TheExit:
        On Error GoTo 0
        IsOpen = fOpen
        Exit Function
        
    EH:
        fOpen = False
        Debug.Print Err.Description
        GoTo TheExit
        
    End Function
    Like I said, this works well for select queries. Typically I am going out to a SQL Server database ( I have all permissions necessary for action queries ).

    TIA
    Attached Files Attached Files

Thread Information

Users Browsing this Thread

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

Similar Threads

  1. Create SQL Query for Pivot Table
    By rbear in forum Excel General
    Replies: 0
    Last Post: 08-13-2008, 03:22 PM
  2. Excel 2007 : Complex query on a pivot table help
    By ZKL in forum Excel General
    Replies: 0
    Last Post: 07-15-2008, 10:26 AM
  3. how do I make Pivot table update automatiocally when I change values
    By soundengineer in forum Excel Formulas & Functions
    Replies: 6
    Last Post: 04-28-2007, 09:22 PM
  4. How to create Pivot Table and Pivot Chart in VBA
    By aijihz in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 03-20-2007, 01:31 PM
  5. ActiveCell is Query Table or Pivot Table?
    By Air_Cooled_Nut in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 10-13-2006, 11:07 AM

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