Guest User

Untitled

a guest
Dec 18th, 2017
194
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.71 KB | None | 0 0
  1. Dim CN As ADODB.Connection
  2. Dim rs As ADODB.Recordset
  3. Dim Ako, Ikaw As String
  4.  
  5.  
  6. Private Sub Command1_Click()
  7. Call TheConn
  8. With ListView1
  9.  
  10. Dim sDatabase As String
  11. Dim sSQL As String
  12. sDatabase = "Driver={MySQL ODBC 3.51 Driver};Server=localhost;Database=sometest; User=root;Password=; "
  13. sSQL = "Select * From users"
  14.  
  15. Set CN = New ADODB.Connection
  16.  
  17. CN.Open sDatabase
  18. Set rs = New ADODB.Recordset
  19. rs.Open sSQL, CN
  20. DTPicker1 = Format(DTPicker1.Value, DTPicker1.CustomFormat)
  21. DTPicker2 = Format(DTPicker2.Value, DTPicker2.CustomFormat)
  22. ListView1.ListItems.Clear
  23.  
  24.  
  25. Set rs = New ADODB.Recordset
  26. Ikaw = "SELECT * FROM users WHERE date_today between '" & Format(DTPicker1.Value, "yyyy/MM/dd ") & "' AND '" & Format(DTPicker2.Value, "yyyy/MM/dd ") & "'"
  27. rs.Open Ikaw, CN, adOpenDynamic, adLockReadOnly
  28. Do While Not rs.EOF
  29. Set aaa = .ListItems.Add(, , rs!firstname)
  30. aaa.SubItems(1) = rs!lastname
  31. aaa.SubItems(2) = rs!age
  32. rs.MoveNext
  33. Loop
  34.  
  35. DTPicker1 = Format(Now, DTPicker1.CustomFormat)
  36. DTPicker2 = Format(Now, DTPicker2.CustomFormat)
  37. End With
  38.  
  39.  
  40. End Sub
  41.  
  42. Private Sub Command2_Click()
  43. Set conn = New ADODB.Connection
  44. conn.ConnectionString = "DRIVER={MySQL ODBC 3.51 Driver};SERVER=localhost;DATABASE=sometest; user=root; password= "
  45. conn.Open
  46.  
  47. aaa = "insert into users(firstname,lastname,age) values('" & Text1 & "','" & Text2 & "','" & Text3 & "')"
  48. conn.Execute aaa
  49.  
  50.  
  51.  
  52.  
  53.  
  54. End Sub
  55.  
  56. Private Sub TheConn()
  57. Set CN = New ADODB.Connection
  58. Ako = "Provider=MICROSOFT.JET.OLEDB.4.0; DATA SOURCE=" & App.Path & "\Lv.mdb"
  59. CN.ConnectionString = Ako
  60. CN.Open
  61. End Sub
  62.  
  63. Private Sub Command3_Click()
  64.  
  65. Dim cnt As New ADODB.Connection
  66. Dim rst As New ADODB.Recordset
  67.  
  68. Dim xlApp As Object
  69. Dim xlWb As Object
  70. Dim xlWs As Object
  71.  
  72.  
  73. Dim recArray As Variant
  74.  
  75. Dim strDB As String
  76. Dim fldCount As Integer
  77. Dim recCount As Long
  78. Dim iCol As Integer
  79. Dim iRow As Integer
  80. Dim aa As String
  81. Dim aal As ListItem
  82.  
  83.  
  84.  
  85.  
  86. Set xlApp = CreateObject("Excel.Application")
  87. Set xlWb = xlApp.Workbooks.Add
  88. Set xlWs = xlWb.Worksheets("Sheet1")
  89.  
  90.  
  91. sDatabase = "Driver={MySQL ODBC 3.51 Driver};Server=localhost;Database=sometest; User=root;Password=; "
  92.  
  93. If CheckBox1.Checked Then
  94. Menu_List.Add ("firstname")
  95. Else
  96. Menu_List.Remove ("firstname")
  97. End If
  98.  
  99. If CheckBox2.Checked Then
  100. Menu_List.Add ("lastname")
  101. Else
  102. Menu_List.Remove ("lastname")
  103. End If
  104.  
  105. If CheckBox3.Checked Then
  106. Menu_List.Add ("age")
  107. Else
  108. Menu_List.Remove ("age")
  109. End If
  110.  
  111. If CheckBox4.Checked Then
  112. Menu_List.Add ("date_today")
  113. Else
  114. Menu_List.Remove ("date_today")
  115. End If
  116. aal = [String].Join(", ", aal.ToArray())
  117.  
  118. sSQL = "SELECT " + MenuList + " FROM users WHERE date_today between '" & Format(DTPicker1.Value, "yyyy/MM/dd ") & "' AND '" & Format(DTPicker2.Value, "yyyy/MM/dd ") & "'"
  119. Set cnt = New ADODB.Connection
  120.  
  121. cnt.Open sDatabase
  122. Set rst = New ADODB.Recordset
  123. rst.Open sSQL, cnt
  124.  
  125.  
  126. ' Display Excel and give user control of Excel's lifetime
  127. xlApp.Visible = True
  128. xlApp.UserControl = True
  129.  
  130. ' Copy field names to the first row of the worksheet
  131. fldCount = rst.Fields.Count
  132.  
  133. For iCols = 0 To fldCount - 1
  134. xlWs.Cells(1, iCols + 1).Value = rst.Fields(iCols).Name
  135. Next
  136.  
  137.  
  138. ' Check version of Excel
  139. If Val(Mid(xlApp.Version, 1, InStr(1, xlApp.Version, ".") - 1)) > 8 Then
  140. 'EXCEL 2000,2002,2003, or 2007: Use CopyFromRecordset
  141.  
  142. ' Copy the recordset to the worksheet, starting in cell A2
  143. xlWs.Cells(2, 1).CopyFromRecordset rst
  144. 'Note: CopyFromRecordset will fail if the recordset
  145. 'contains an OLE object field or array data such
  146. 'as hierarchical recordsets
  147.  
  148. Else
  149. 'EXCEL 97 or earlier: Use GetRows then copy array to Excel
  150.  
  151. ' Copy recordset to an array
  152. recArray = rst.GetRows
  153. 'Note: GetRows returns a 0-based array where the first
  154. 'dimension contains fields and the second dimension
  155. 'contains records. We will transpose this array so that
  156. 'the first dimension contains records, allowing the
  157. 'data to appears properly when copied to Excel
  158.  
  159. ' Determine number of records
  160.  
  161. recCount = UBound(recArray, 2) + 1 '+ 1 since 0-based array
  162.  
  163.  
  164. ' Check the array for contents that are not valid when
  165. ' copying the array to an Excel worksheet
  166. For iCol = 0 To fldCount - 1
  167. For iRow = 0 To recCount - 1
  168. ' Take care of Date fields
  169. If IsDate(recArray(iCol, iRow)) Then
  170. recArray(iCol, iRow) = Format(recArray(iCol, iRow))
  171. ' Take care of OLE object fields or array fields
  172. ElseIf IsArray(recArray(iCol, iRow)) Then
  173. recArray(iCol, iRow) = "Array Field"
  174. End If
  175. Next iRow 'next record
  176. Next iCol 'next field
  177.  
  178. ' Transpose and Copy the array to the worksheet,
  179. ' starting in cell A2
  180. xlWs.Cells(2, 1).Resize(recCount, fldCount).Value = _
  181. TransposeDim(recArray)
  182. End If
  183.  
  184. ' Auto-fit the column widths and row heights
  185. xlApp.Selection.CurrentRegion.Columns.AutoFit
  186. xlApp.Selection.CurrentRegion.Rows.AutoFit
  187.  
  188. ' Close ADO objects
  189. rst.Close
  190. cnt.Close
  191. Set rst = Nothing
  192. Set cnt = Nothing
  193.  
  194. ' Release Excel references
  195. Set xlWs = Nothing
  196. Set xlWb = Nothing
  197.  
  198. Set xlApp = Nothing
  199.  
  200.  
  201.  
  202. End Sub
  203.  
  204.  
  205.  
  206. Private Sub Command4_Click()
  207. ListView1.ListItems.Clear
  208.  
  209. End Sub
  210.  
  211. Function TransposeDim(v As Variant) As Variant
  212. ' Custom Function to Transpose a 0-based array (v)
  213.  
  214. Dim X As Long, Y As Long, Xupper As Long, Yupper As Long
  215. Dim tempArray As Variant
  216.  
  217. Xupper = UBound(v, 2)
  218. Yupper = UBound(v, 1)
  219.  
  220. ReDim tempArray(Xupper, Yupper)
  221. For X = 0 To Xupper
  222. For Y = 0 To Yupper
  223. tempArray(X, Y) = v(Y, X)
  224. Next Y
  225. Next X
  226.  
  227. TransposeDim = tempArray
  228.  
  229.  
  230. End Function
  231.  
  232.  
  233. Private Sub Form_Load()
  234. Dim sDatabase As String
  235. Dim sSQL As String
  236. sDatabase = "Driver={MySQL ODBC 3.51 Driver};Server=localhost;Database=sometest; User=root;Password=; "
  237. sSQL = "Select * From users"
  238.  
  239. Set CN = New ADODB.Connection
  240.  
  241. CN.Open sDatabase
  242. Set rs = New ADODB.Recordset
  243.  
  244. rs.Open sSQL, CN
  245.  
  246. Do Until (rs.EOF)
  247. List1.AddItem rs.Fields("firstname")
  248. rs.MoveNext
  249. Loop
  250.  
  251. DTPicker1 = Format(Now, DTPicker1.CustomFormat)
  252. DTPicker2 = Format(Now, DTPicker2.CustomFormat)
  253.  
  254.  
  255. Image1.Height = Me.Height
  256. Image1.Width = Me.Width
  257.  
  258.  
  259.  
  260. End Sub
Add Comment
Please, Sign In to add comment