รบกวนผู้รู้ครับ จากโค้ดด้านล่างมีคำถามคือว่า
1.เราสามารถปรับเปลี่ยน Format ของวันที่ตาม Format ที่ต้องการได้ไหมครับ จากที่ได้ export ออกมาเป็น .sql จะได้เป็น
INSERT INTO checktime ('PIN','CHECKTIME') VALUES
('PIN','CHECKTIME'),
17,
5/14/2020 3:32:05 PM),
16,
5/14/2020 3:32:00 PM),
5,
5/14/2020 3:30:51 PM)
แต่ format ที่ต้องการจะเป็น dd/mm/yyyy h:mm ครับ
2.สามารถกำหนดชื่อไฟล์ที่จะ export ออกมาจาก "myfilepath = "C:\
mysql.sql" ให้ run ตามวันที่ของ Data ใน Column "CHECKTIME" ได้ไหมครับและปรับ Format เป็น yyyymmdd.sql
Code: Select all
Private Sub CommandButton3_Click()
'creates the file
Dim myfilepath As String
myfilepath = "C:\mysql.sql"
Dim fso As Object
Set fso = CreateObject("Scripting.FileSystemObject")
Dim myFile As Object
Set myFile = fso.createtextfile(myfilepath)
'writes stuff to the file
With myFile
Dim sqlstr As String
sqlstr = "INSERT INTO checktime ("
Dim colcount As Integer
colcount = 1
With ThisWorkbook.Sheets("Summary")
Do Until .Cells(1, colcount).Value = ""
sqlstr = sqlstr & "'" & .Cells(1, colcount).Value & "',"
colcount = colcount + 1
Loop
sqlstr = Left(sqlstr, Len(sqlstr) - 1) & ") VALUES " & Chr(13) & "("
Dim rCount As Integer
rCount = 1
Do Until .Cells(rCount, 1).Value = ""
colcount = 1
Do Until .Cells(1, colcount).Value = ""
If IsNumeric(.Cells(rCount, colcount).Value) Then
sqlstr = sqlstr & "" & .Cells(rCount, colcount).Value & _
","
ElseIf IsDate(.Cells(rCount, colcount).Value) Then
sqlstr = sqlstr & "" & .Cells(rCount, colcount).Value & _
","
Else
sqlstr = sqlstr & "'" & .Cells(rCount, colcount).Value & _
"',"
End If
colcount = colcount + 1
Loop
sqlstr = Left(sqlstr, Len(sqlstr) - 1) & ")," & Chr(13)
rCount = rCount + 1
Loop
sqlstr = Left(sqlstr, Len(sqlstr) - 2)
End With
.writeline sqlstr
End With
'closes the file
myFile.Close
Set myFile = Nothing
End Sub
ขอบคุณล่วงหน้าครับ
