forked from AllenMattson/VBA_personal
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ImportToMSSQL.vb
45 lines (33 loc) · 1.52 KB
/
ImportToMSSQL.vb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
Option Explicit
Sub GenerateData()
Dim conn As New ADODB.Connection
Dim l_row As Long
Dim s_username As String
Dim s_date As String
Dim s_time As String
Dim s_location As String
Dim s_status As String
With ActiveSheet
conn.Open "Provider=SQLOLEDB;Data Source=GRO-PC;Initial Catalog=LogData;Integrated Security=SSPI;"
l_row = last_row_with_data(1, ActiveSheet) + 1
.Cells(l_row, 1) = Environ("username")
.Cells(l_row, 2) = Date
.Cells(l_row, 3) = Time
.Cells(l_row, 4) = Application.ActiveWorkbook.FullName
.Cells(l_row, 5) = make_random(2, 6)
s_username = .Cells(l_row, 1)
s_date = .Cells(l_row, 2)
s_time = .Cells(l_row, 3)
s_location = .Cells(l_row, 4)
s_status = .Cells(l_row, 5)
conn.Execute "insert into dbo.LogTable (UserName, CurrentDate, CurrentTime, CurrentLocation, Status) values ('" & s_username & "', '" & s_date & "', '" & s_time & "', '" & s_location & "','" & s_status & "')"
conn.Close
Set conn = Nothing
End With
End Sub
Public Function last_row_with_data(ByVal lng_column_number As Long, shCurrent As Variant) As Long
last_row_with_data = shCurrent.Cells(Rows.Count, lng_column_number).End(xlUp).row
End Function
Public Function make_random(down As Integer, up As Integer)
make_random = Int((up - down + 1) * Rnd + down)
End Function