Dear All,
Lately I have been posting some question but could not get any answer. I am not sure that my questions were not clear or not but I managed to fix my problems by myself. However, I don’t know why I could not get any help. Please let me know if there is something wrong with my post this time. Here is my problem.
I wrote a code that extracts data from sql server. Everything works fine except this funny issue. Whenever the date is 30 Dec 2014 or 31 Dec 2014 the month appears as January. How can this happen? Any idea? For security issue I changed my server name and the database name. Below is my code:
On the other hand I also try this which is only to test the month name of 31 Dec 2014, but I still got January:
Lately I have been posting some question but could not get any answer. I am not sure that my questions were not clear or not but I managed to fix my problems by myself. However, I don’t know why I could not get any help. Please let me know if there is something wrong with my post this time. Here is my problem.
I wrote a code that extracts data from sql server. Everything works fine except this funny issue. Whenever the date is 30 Dec 2014 or 31 Dec 2014 the month appears as January. How can this happen? Any idea? For security issue I changed my server name and the database name. Below is my code:
Code:
Sub GetingItFromServer()
Dim DBFullName, TableName As String
Dim TargetRange As Range
Dim Conn As ADODB.Connection, intColIndex As Integer
Dim cel As Range
Dim TD As Long
Dim qdate1 As Double
Dim qdate2 As Double
Dim LastRow As Long
Application.ScreenUpdating = False
Application.DisplayAlerts = False
qdate1 = Range("trddate1").Value
qdate2 = Range("trddate2").Value
'MsgBox qdate1 & " " & qdate2
'Exit Sub
Sheets("TableData").Range("A2:J1048576").ClearContents
Sheets("TableData").Select
Columns("A:H").AutoFilter
Range("A2").Select
Selection.Activate
Set TargetRange = Range("A2")
Set Conn = New ADODB.Connection
Conn.Open "driver={SQL Server};" & _
"server=TrVST1;database=OverAllData;"
Set RecSet = New Recordset
RecSet.Open "SELECT hist_shift.table_id,hist_shift.game_date, {fn MONTHNAME(hist_shift.game_date)} FROM hist_shift hist_shift " & _
"WHERE hist_shift.game_date>='" & qdate1 & "' And hist_shift.game_date<='" & qdate2 & "' ORDER BY hist_shift.pit_name", Conn, , , adCmdText
TargetRange.CopyFromRecordset RecSet
RecSet.Close
Set RecSet = Nothing
Conn.Close
Set Conn = Nothing
LastRow = Sheets("TableData").Range("A" & Sheets("TableData").Rows.Count).End(xlUp).Row
With ActiveWorkbook.Worksheets("TableData").Sort
.SetRange Range("A2:J" & LastRow)
.Header = xlGuess
.MatchCase = False
.Orientation = xlTopToBottom
.Apply
End With
Columns("B:B").NumberFormat = "[$-409]d-mmm-yy;@"
Columns("A:H").AutoFilter
End Sub
Code:
RecSet.Open "SELECT hist_shift.table_id,hist_shift.game_date, {fn MONTHNAME(42004)} FROM hist_shift hist_shift
Last edited: