+ Reply to Thread
Results 1 to 4 of 4

VBA Excel to Access Query Creation

Hybrid View

randell.graybill VBA Excel to Access Query... 07-28-2011, 12:26 AM
snb Re: VBA Excel to Access Query... 07-28-2011, 03:56 AM
randell.graybill Re: VBA Excel to Access Query... 07-28-2011, 08:47 PM
romperstomper Re: VBA Excel to Access Query... 07-28-2011, 04:41 AM
  1. #1
    Forum Contributor
    Join Date
    04-03-2009
    Location
    USA, California
    MS-Off Ver
    Excel 2007
    Posts
    385

    VBA Excel to Access Query Creation

    I have the following macro that I created using Access VBA due to the fact that I am very much unfamiliar with Access VBA but I am hoping to find someone here that knows both.

    The question is how would I convert the module to do the same thing but from excel without having to open the database?

    
    Private Sub cmbSubmit_Click()
        Dim TheDbPath As String
        Dim QueryName As String
        Dim SQLStr As String
        Dim Crt1 As String
        Dim SqlStrWhere As String
        
        Crt1 = "ABS"
        'First we'll set some variables:
        SqlStrWhere = "Weekof17July2011.Sunday=""ABS"" Or Weekof17July2011.Sunday=""Late"" Or Weekof17July2011.Sunday=""LE"""
    
        SQLStr = "SELECT * FROM Weekof17July2011 WHERE " & SqlStrWhere
        
      
        TheDbPath = "C:\Documents and Settings\randell.graybill\My Documents\Attendance.mdb"
        QueryName = Forms!CreateQuery.cBoxTableNames.Value & " Query"
        'SQLStr = "SELECT * FROM " & Me.cBoxTableNames.Value
      
          Dim cn As ADODB.Connection
        Dim cat As ADOX.Catalog
        Dim cmd As ADODB.Command
        Set cn = New ADODB.Connection
        
        
        cn.Open "Provider=Microsoft.Jet.OLEDB.4.0; " & _
        "Data Source=" & TheDbPath & ";"
         
        If cn.State <> 1 Then
            MsgBox ("Problem with connection")
            Exit Sub
        End If
         
        Set cat = New ADOX.Catalog
        Set cmd = New ADODB.Command
         
        cat.ActiveConnection = cn
         
        cmd.CommandText = SQLStr
        'On Error Resume Next
        
        cat.Views.Append QueryName, cmd
        
        If Err.Number = -2147217816 Then
        Err.Clear
        MsgBox "This Query already exists!"
        End If
         
        Set cat = Nothing
        Set cmd = Nothing
        cn.Close
        Set cn = Nothing
    
        
    
    DoCmd.Close acForm, Forms!CreateQuery.Name
    
    DoCmd.OpenForm "AttendanceForm"
    
    End Sub

  2. #2
    Forum Expert snb's Avatar
    Join Date
    05-09-2010
    Location
    VBA
    MS-Off Ver
    Redhat
    Posts
    5,649

    Re: VBA Excel to Access Query Creation

    Did you consider using a databasequery ?



  3. #3
    Forum Contributor
    Join Date
    04-03-2009
    Location
    USA, California
    MS-Off Ver
    Excel 2007
    Posts
    385

    Re: VBA Excel to Access Query Creation

    Quote Originally Posted by snb View Post
    Did you consider using a databasequery ?
    What is that? Very new to access and as always trying to complete a project beyond my understanding...best way to learn right?

  4. #4
    Forum Expert romperstomper's Avatar
    Join Date
    08-13-2008
    Location
    England
    MS-Off Ver
    365, varying versions/builds
    Posts
    22,017

    Re: VBA Excel to Access Query Creation

    Other than the stuff where you supply values from a form and manipulate forms at the end, that code ought to work in Excel.
    Everyone who confuses correlation and causation ends up dead.

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

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