Advertisement
Guest User

Untitled

a guest
Jan 11th, 2016
227
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.08 KB | None | 0 0
  1. Private Sub SubName_Exit(ByVal Cancel As MSForms.ReturnBoolean)
  2. ' MsgBox ("On exit")
  3. ' SubID.Caption = GetSubID(SubName.Value)
  4. ' Address.Value
  5. ' City.Value
  6. ' State.Value
  7. ' Zipcode.Value
  8. End Sub
  9.  
  10.  
  11.  
  12. Private Sub UserForm_Initialize()
  13. ' Dim rs As ADODB.Recordset
  14. ' Dim cn As ADODB.Connection
  15. Dim SQL_Query As String
  16.  
  17. With SubSearch
  18. .StartUpPosition = 0
  19. .Left = Application.Left + (0.5 * Application.Width) - (0.5 * .Width)
  20. .Top = Application.Top + (0.5 * Application.height) - (0.5 * .height)
  21. End With
  22.  
  23. ' This section determines the initial values for the textboxes
  24.  
  25. SubName.Value = ActiveCell.Value
  26. Address.Value = ActiveCell.Offset(1, 0).Value
  27. City.Value = ExtractCity(ActiveCell.Offset(2, 0).Value)
  28. State.Value = ExtractState(ActiveCell.Offset(2, 0).Value)
  29. Zipcode.Value = ExtractZipcode(ActiveCell.Offset(2, 0).Value)
  30. ActiveCell.ID = "SubID: " & GetSubID(SubName.Value)
  31. SubID.Caption = GetSubID(ActiveCell.Value)
  32. ' SubName.AddItem "<< (+) ADD A NEW SUBCONTRACTOR >>", 0
  33. ' MsgBox ("There are " & SubName.ListCount & " list items")
  34.  
  35. End Sub
  36.  
  37. Private Sub SubName_KeyUp(ByVal KeyCode As MSForms.ReturnInteger, ByVal Shift As Integer)
  38.  
  39. ' Do While SubName.ListCount > 0
  40. ' SubName.RemoveItem (0)
  41. ' Loop
  42.  
  43.  
  44. If SubName.Value <> "" And Len(SubName.Value) > 0 Then
  45. Call mysqlSearch(SubName.Value, "entities", "Name")
  46. SubName.DropDown
  47. End If
  48.  
  49.  
  50.  
  51. 'Try on backspace key press, bring attention to box, (this should hide the combobox)
  52. 'Then on select of contractor get SubID and associated address.
  53. 'Allow for up and down arrow selection`
  54.  
  55. End Sub
  56.  
  57.  
  58.  
  59.  
  60. Private Sub OK_Click()
  61. 'Adds selected Subcontractor to current cell
  62. ActiveCell = SubName.Value
  63.  
  64. 'Adds address to cell below active cell
  65. ActiveCell.Offset(1, 0).Value = Address.Value
  66.  
  67. ActiveCell.Offset(2, 0).Value = City.Value & ", " & State.Value & " " & Zipcode.Value
  68. ActiveCell.ID = "SubID: " & GetSubID(SubName.Value)
  69. Call AddUpdateAddress(SubID.Caption, Address.Value, City.Value, State.Value, Zipcode.Value, ActiveCell.Offset(3, 0).Value)
  70. Unload Me
  71.  
  72. End Sub
  73. Private Sub CancelButton_Click()
  74. Unload Me
  75. End Sub
  76.  
  77. Function AddUpdateAddress(SubID As String, Address As String, City As String, State As String, Zipcode As String, Website As String)
  78. Dim rs As ADODB.Recordset
  79. Dim cn As ADODB.Connection
  80. Dim mysqlstring As String
  81.  
  82. Set rs = New ADODB.Recordset
  83. Set cn = New ADODB.Connection
  84. cn.Open "DRIVER={MySQL ODBC 5.1 Driver};" & _
  85. "SERVER=127.0.0.1;" & _
  86. "DATABASE=progc_db;" & _
  87. "USER=admin;" & _
  88. "PASSWORD=admin;" & _
  89. "Option=3"
  90.  
  91. mysqlstring = "INSERT IGNORE INTO mainaddress (EntityID,Address,City,State,Zipcode,Website) VALUES ('" & SubID & "','" & Address & "','" & City & "','" & State & "','" & Zipcode & "','" & Website & "');"
  92. ' Range("A1").Value = mysqlstring
  93. rs.Open mysqlstring, cn, adOpenStatic
  94.  
  95. cn.Close
  96.  
  97. End Function
  98.  
  99.  
  100.  
  101.  
  102. Public Function mysqlSearch(SearchString As String, TableName As String, ColumnName As String)
  103. Dim rs As ADODB.Recordset
  104. Dim cn As ADODB.Connection
  105. Dim ListItem As String
  106. Dim i As Integer
  107. Dim ListCount As Integer
  108. Dim SearchCount As Integer
  109.  
  110. SearchString = esc(SearchString)
  111. ' MsgBox (SearchString)
  112.  
  113. Set rs = New ADODB.Recordset
  114. Set cn = New ADODB.Connection
  115. cn.Open "DRIVER={MySQL ODBC 5.1 Driver};" & _
  116. "SERVER=127.0.0.1;" & _
  117. "DATABASE=progc_db;" & _
  118. "USER=admin;" & _
  119. "PASSWORD=admin;" & _
  120. "Option=3"
  121.  
  122. ListCount = SubName.ListCount
  123. mysqlstring = "SELECT " & ColumnName & ",EntityID" & " FROM " & TableName & " WHERE " & ColumnName & " LIKE '" & SearchString & "%';"
  124. ' MsgBox (mysqlString)
  125. rs.Open mysqlstring, cn, adOpenStatic
  126.  
  127. ' ADD CONTRACTOR ID
  128. ' CREATE READ-ONLY FIELDS
  129. ' RECORD SELECTION OF ITEM AND POPULATE FIELDS
  130. ' ALLOW KEY DOWN LOOP
  131.  
  132. ' Address.SetFocus
  133. ' Focus is shifted away and then back to combobox to fix visual bug
  134. ' MsgBox (SubName.ListCount)
  135.  
  136. If rs.EOF = True Then
  137. ' Address.SetFocus
  138. SubName.SetFocus
  139. SubName.Clear
  140. ' Focus is shifted away and then back to combobox to fix visual bug
  141.  
  142. Else
  143. SearchResults = rs.GetString(, , ",", Chr(13))
  144. ' MsgBox (SearchResults)
  145. SearchCount = UBound(Split(SearchResults, Chr(13)))
  146. ' MsgBox SearchCount
  147.  
  148. 'Removes all list items in combobox
  149. With SubName
  150. If ListCount > 0 Then
  151. For i = 0 To ListCount - 1
  152. .RemoveItem (0)
  153. Next i
  154. End If
  155. End With
  156. SubName.Clear
  157.  
  158. 'Adds initial top list item of combobox
  159. SubName.AddItem "<< ADD A NEW SUBCONTRACTOR >>", 0
  160.  
  161. 'Adds query results as combobox list items
  162. With SubName
  163. For j = 0 To SearchCount - 1
  164. ListItem = Split(SearchResults, Chr(13))(j)
  165. ListItem = Split(ListItem, ",")(0)
  166. ' MsgBox (ListItem)
  167. .AddItem ListItem, (j + 1)
  168. Next j
  169. End With
  170.  
  171.  
  172. 'SubName.SetFocus
  173.  
  174. End If
  175. rs.Close
  176. End Function
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement