thanks for your reply, i did everything as suggested but it keeps coming up with a runtime error 1004?
any idea what the problem is?![]()
thanks for your reply, i did everything as suggested but it keeps coming up with a runtime error 1004?
any idea what the problem is?![]()
firstly what format is the date field in the sql table?
Mr MaGoo
Magoo.Inc MMVII
If i've helped please add to my Rep by Clicking on the Blue Scales in the top right hand corner of the post
i've changed the month field to date field to test the formatting command
this is what i have now
i added a message box to test the date output and that is ok, and i also specified the sheet for the date input - but the problem is at the commandtext section for some reason?![]()
Sub Macro1() Dim MyDate As Date MyDate = Worksheets("Sheet1").Range("D2").Value MsgBox Format(MyDate, "YYYY-MM-DD") With ActiveWorkbook.Connections("sales").ODBCConnection .BackgroundQuery = True .CommandText = Array( _ "SELECT * FROM `table1`.`pivot-1month` WHERE `Date` = " & MyDate) .CommandType = xlCmdSql .Connection = "ODBC;DSN=Database;" .RefreshOnFileOpen = False .SavePassword = False .SourceConnectionFile = "" .ServerCredentialsMethod = xlCredentialsMethodIntegrated .AlwaysUseConnectionFile = False End With With ActiveWorkbook.Connections("sales") .Name = "salesdata" .Description = "Last Week" End With End Sub
Try This
and if that doesn't wor remove the "Array" crap and its brackets then try that![]()
Sub Macro1() Dim MyDate As Date MyDate = Worksheets("Sheet1").Range("D2").Value MsgBox Format(MyDate, "YYYY-MM-DD") With ActiveWorkbook.Connections("sales").ODBCConnection .BackgroundQuery = True .CommandText = Array( _ "SELECT * FROM `table1`.`pivot-1month` WHERE `Date` = " & Format(MyDate, "YYYY-MM-DD")) .CommandType = xlCmdSql .Connection = "ODBC;DSN=Database;" .RefreshOnFileOpen = False .SavePassword = False .SourceConnectionFile = "" .ServerCredentialsMethod = xlCredentialsMethodIntegrated .AlwaysUseConnectionFile = False End With With ActiveWorkbook.Connections("sales") .Name = "salesdata" .Description = "Last Week" End With End Sub
still no luck runtime 1004 highlighting that commandtext line, do i possibly need more info on the odbc connection somewhere - not sure what the issue is at all![]()
Its hard to help when idont have the connection lol
once last try:
![]()
Sub Macro1() Dim MyDate As Date MyDate = Worksheets("Sheet1").Range("D2").Value MsgBox Format(MyDate, "YYYY-MM-DD") With ActiveWorkbook.Connections("sales").ODBCConnection .BackgroundQuery = True .CommandText = Array( _ "SELECT * FROM `table1`.`pivot-1month` WHERE `Date` = " & "'" & Format(MyDate, "YYYY-MM-DD") & "'") .CommandType = xlCmdSql .Connection = "ODBC;DSN=Database;" .RefreshOnFileOpen = False .SavePassword = False .SourceConnectionFile = "" .ServerCredentialsMethod = xlCredentialsMethodIntegrated .AlwaysUseConnectionFile = False End With With ActiveWorkbook.Connections("sales") .Name = "salesdata" .Description = "Last Week" End With End Sub
i added this line to output to a message box to see what the query looks like and it looks fine
really stumped! i appreciate you taking the time to help thought this would be straight forward :s![]()
Sub Macro1() Dim MyDate As Date MyDate = Worksheets("Sheet1").Range("D2").Value MyDate2 = Format(MyDate, "YYYY-MM-DD") brackets = "'" Test1 = "SELECT * FROM pivot1month WHERE Date = '" & MyDate2 & "'" MsgBox Test1 With ActiveWorkbook.Connections("sales").ODBCConnection .BackgroundQuery = True .CommandText = "SELECT * FROM pivot1month WHERE Date = '" & Format(MyDate, "YYYY-MM-DD") & "'" .CommandType = xlCmdSql .Connection = "ODBC;DSN=Database;" .RefreshOnFileOpen = False .SavePassword = False .SourceConnectionFile = "" .ServerCredentialsMethod = xlCredentialsMethodIntegrated .AlwaysUseConnectionFile = False End With With ActiveWorkbook.Connections("sales") .Name = "sales" .Description = "Last Week" End With End Sub
Last edited by cacoyle45; 05-26-2011 at 11:12 AM. Reason: addition
what about:
![]()
Sub Macro1() Dim MyDate As Date MyDate = Worksheets("Sheet1").Range("D2").Value MyDate2 = Format(MyDate, "YYYY-MM-DD") brackets = "'" Test1 = "SELECT * FROM pivot1month WHERE Date = '" & MyDate2 & "'" MsgBox Test1 With ActiveWorkbook.Connections("sales").ODBCConnection .BackgroundQuery = True .CommandText = Test1 .CommandType = xlCmdSql .Connection = "ODBC;DSN=Database;" .RefreshOnFileOpen = False .SavePassword = False .SourceConnectionFile = "" .ServerCredentialsMethod = xlCredentialsMethodIntegrated .AlwaysUseConnectionFile = False End With With ActiveWorkbook.Connections("sales") .Name = "sales" .Description = "Last Week" End With End Sub
There are currently 1 users browsing this thread. (0 members and 1 guests)
Bookmarks