Hi, I'm trying to run this code, which gets information from an access database and then displays it in excel. This runs fine if I set the red text "WHERE IndMotor.KV =" a number, say 4. But I really need to have the query select one of the values from the DISTINCT (Blue) query. I thought I had gotten my variables right, but it seems to not work. If you could help me at all, that would be appreciated.
Range("BU4").Select
With ActiveSheet.QueryTables.Add(Connection:=temp$, Destination:=Range("BU4"))
.CommandText = "SELECT Distinct IndMotor.KV" _
& Chr(13) & "" & Chr(10) & "FROM IndMotor IndMotor" _
& Chr(13) & "" & Chr(10) & "ORDER BY IndMotor.KV DESC;"
.Name = "Query from MS Access Database"
.FieldNames = True
.RowNumbers = False
.FillAdjacentFormulas = False
.PreserveFormatting = True
.RefreshOnFileOpen = False
.BackgroundQuery = False
.RefreshStyle = xlOverwriteCells
.SavePassword = False
.SaveData = True
.AdjustColumnWidth = False
.RefreshPeriod = 0
.PreserveColumnInfo = True
.Refresh BackgroundQuery:=False
End With
Dim Mtr1 As Variant
Dim Mtr2 As Variant
Dim Mtr3 As Variant
Dim Mtr4 As Variant
Dim Mtr5 As Variant
Mtr1 = Worksheets("Data_IndMtr").Cells(73, 5)
Mtr2 = Worksheets("Data_IndMtr").Cells(73, 6)
Mtr3 = Worksheets("Data_IndMtr").Cells(73, 7)
Mtr4 = Worksheets("Data_IndMtr").Cells(73, 8)
Mtr5 = Worksheets("Data_IndMtr").Cells(73, 9)
Range("K4").Select
With ActiveSheet.QueryTables.Add(Connection:=temp$, Destination:=Range("K4"))
.CommandText = "SELECT TOP 5 IndMotor.ID, IndMotor.KV, IndMotor.HP" _
& Chr(13) & "" & Chr(10) & "FROM IndMotor IndMotor" _
& Chr(13) & "" & Chr(10) & "WHERE (((IndMotor.KV)='Mtr1'))" _
& Chr(13) & "" & Chr(10) & "ORDER BY IndMotor.HP DESC;"
.Name = "Query from MS Access Database"
.FieldNames = True
.RowNumbers = False
.PreserveFormatting = True
.RefreshOnFileOpen = False
.BackgroundQuery = False
.RefreshStyle = xlOverwriteCells
.SavePassword = False
.AdjustColumnWidth = False
.RefreshPeriod = 0
.PreserveColumnInfo = True
.Refresh BackgroundQuery:=False
End With
Range("BU4").Select
With ActiveSheet.QueryTables.Add(Connection:=temp$, Destination:=Range("BU4"))
.CommandText = "SELECT Distinct IndMotor.KV" _
& Chr(13) & "" & Chr(10) & "FROM IndMotor IndMotor" _
& Chr(13) & "" & Chr(10) & "ORDER BY IndMotor.KV DESC;"
.Name = "Query from MS Access Database"
.FieldNames = True
.RowNumbers = False
.FillAdjacentFormulas = False
.PreserveFormatting = True
.RefreshOnFileOpen = False
.BackgroundQuery = False
.RefreshStyle = xlOverwriteCells
.SavePassword = False
.SaveData = True
.AdjustColumnWidth = False
.RefreshPeriod = 0
.PreserveColumnInfo = True
.Refresh BackgroundQuery:=False
End With
Dim Mtr1 As Variant
Dim Mtr2 As Variant
Dim Mtr3 As Variant
Dim Mtr4 As Variant
Dim Mtr5 As Variant
Mtr1 = Worksheets("Data_IndMtr").Cells(73, 5)
Mtr2 = Worksheets("Data_IndMtr").Cells(73, 6)
Mtr3 = Worksheets("Data_IndMtr").Cells(73, 7)
Mtr4 = Worksheets("Data_IndMtr").Cells(73, 8)
Mtr5 = Worksheets("Data_IndMtr").Cells(73, 9)
Range("K4").Select
With ActiveSheet.QueryTables.Add(Connection:=temp$, Destination:=Range("K4"))
.CommandText = "SELECT TOP 5 IndMotor.ID, IndMotor.KV, IndMotor.HP" _
& Chr(13) & "" & Chr(10) & "FROM IndMotor IndMotor" _
& Chr(13) & "" & Chr(10) & "WHERE (((IndMotor.KV)='Mtr1'))" _
& Chr(13) & "" & Chr(10) & "ORDER BY IndMotor.HP DESC;"
.Name = "Query from MS Access Database"
.FieldNames = True
.RowNumbers = False
.PreserveFormatting = True
.RefreshOnFileOpen = False
.BackgroundQuery = False
.RefreshStyle = xlOverwriteCells
.SavePassword = False
.AdjustColumnWidth = False
.RefreshPeriod = 0
.PreserveColumnInfo = True
.Refresh BackgroundQuery:=False
End With