Advertisement
Guest User

Untitled

a guest
Dec 17th, 2018
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
VBScript 12.00 KB | None | 0 0
  1. 'Dim dbFile
  2. 'dbFile = txtAddress.text
  3. strDSN = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & itxtAddress.text
  4.  
  5. Const adVarWChar = 202
  6. Const adSingle = 4
  7. Const adLockOptimistic = 3
  8. Const adOpenStatic = 3
  9. Const adOpenDynamic = 2
  10. Const adCmdTable = &H0002
  11. Dim actArray
  12. Dim position
  13. Dim myArray(15)
  14.  
  15.  
  16. '-------------------------------------------------------------------------------
  17. Sub Initform
  18.    [_Template].color = clmenu
  19. lstID.clear
  20. lstImgURL.clear
  21. lstImgProxyURL.clear
  22. lstBox.clear
  23.  
  24. call readMDB("tbl_Election", "ID", "Candidate", "Image_URL", "Image_Proxy_URL")
  25.  
  26. End Sub
  27.  
  28. '-------------------------------------------------------------------------------
  29. ' READ DATABASE AND POPULATE IT TO LISTBOX
  30. '-------------------------------------------------------------------------------
  31. Sub readMDB(dbTable, dbcol1, dbcol2, dbcol3, dbcol4)
  32.  
  33. Set objConnection = CreateObject("ADODB.Connection")
  34. Set objRecordSet = CreateObject("ADODB.Recordset")
  35.  
  36. objConnection.Open strDSN
  37.  
  38. objRecordSet.Open "SELECT * FROM " & dbTable & " ORDER BY ID ASC", _
  39.         objConnection, adOpenDynamic, adLockOptimistic
  40. With objRecordSet
  41.  
  42.     If Not .BOF And Not .EOF Then
  43.  
  44.    if lstBox.items.count > 0 then
  45.         objRecordSet.MoveFirst
  46.    end if
  47.  
  48. Do Until objRecordSet.EOF
  49.     if dbcol1 <> false then lstID.Items.Add(objRecordSet.Fields.Item(dbcol1))
  50.     if dbcol2 <> false then lstBox.Items.Add(objRecordSet.Fields.Item(dbcol2))
  51.     if dbcol3 <> false then lstImgURL.Items.Add(objRecordSet.Fields.Item(dbcol3))
  52.     if dbcol4 <> false then lstImgProxyURL.Items.Add(objRecordSet.Fields.Item(dbcol4))
  53.     objRecordSet.MoveNext
  54. Loop
  55.    End if
  56. End With
  57. objRecordSet.Close
  58. objConnection.Close
  59.  
  60. End Sub
  61. '-------------------------------------------------------------------------------
  62. ' WRITE LISTBOX TO DATABASE
  63. '-------------------------------------------------------------------------------
  64. Sub writeMDB(dbTable, dbcol1, dbcol2, dbcol3, dbcol4)
  65. Dim x
  66. x = 1
  67. Set rs = CreateObject("ADODB.Recordset")
  68.     rs.Open dbTable, strDSN, adOpenDynamic, adLockOptimistic, adCmdTable
  69.         DO WHILE NOT rs.EOF
  70.  
  71.             if dbcol1 <> false then rs(dbcol1).value = x
  72.             if dbcol2 <> false then rs(dbcol2).value = lstBox.items(x-1)
  73.             if dbcol3 <> false then rs(dbcol3).value = lstImgURL.items(x-1)
  74.             if dbcol4 <> false then rs(dbcol4).value = lstImgProxyURL.items(x-1)
  75.  
  76.             x = x + 1
  77.             rs.MoveNext
  78.         Loop
  79.  
  80.    rs.close
  81.    Set rs = Nothing
  82. End sub
  83. '-------------------------------------------------------------------------------
  84. ' SELECTING ITEM IN LISTBOX
  85. '-------------------------------------------------------------------------------
  86. Sub lstBoxClick(Sender)
  87.  
  88.         lstID.itemindex = lstBox.itemindex
  89.         lstImgURL.itemindex = lstBox.itemindex
  90.         lstImgProxyURL.itemindex = lstBox.itemindex
  91.  
  92. End sub
  93.  
  94. '-------------------------------------------------------------------------------
  95. Sub emptyCheck(txtbox)
  96.         if Len(Trim(txtbox)) = 0 then
  97.            cmdAdd.enabled = false
  98.         else
  99.            cmdAdd.enabled = true
  100.         end if
  101. End sub
  102.  
  103. '-------------------------------------------------------------------------------
  104. ' IS LAST ITEM SELECTED?
  105. '-------------------------------------------------------------------------------
  106. Function lastItem
  107.     If lstBox.itemindex = lstBox.items.count -1 then
  108.        lastItem = true
  109.     else
  110.        lastItem = false
  111.     end if
  112. End Function
  113. '-------------------------------------------------------------------------------
  114. ' IS FIRST ITEM SELECTED?
  115. '-------------------------------------------------------------------------------
  116. Function firstItem
  117.     If lstBox.itemindex = 0 or lstBox.items.count = 0 then
  118.        firstItem = true
  119.     else
  120.        firstItem = false
  121.     end if
  122. End Function
  123. '-------------------------------------------------------------------------------
  124. Sub itxtAddChange(Sender)
  125.    call emptyCheck(itxtAdd.text)
  126. End sub
  127. '-------------------------------------------------------------------------------
  128. Sub reorderButtons
  129.         if lstBox.items.count > 1 then
  130.                 cmdUp.enabled = true
  131.                 cmdDown.enabled = true
  132.         else
  133.                 cmdUp.enabled = false
  134.                 cmdDown.enabled = false
  135.         end if
  136. End sub
  137. '-------------------------------------------------------------------------------
  138. ' ADD NEW RECORD TO THE DATABASE
  139. '-------------------------------------------------------------------------------
  140. Sub cmdAddClick(Sender)
  141.  
  142. Set rs = CreateObject("ADODB.Recordset")
  143. position = lstBox.itemindex
  144.         rs.Open "tbl_Election", strDSN, adOpenDynamic, adLockOptimistic, adCmdTable
  145.         rs.AddNew
  146.         rs("Candidate").value = itxtAdd.text
  147.         rs("Image_URL").value = ithumbIMG.PicFilename
  148.         rs("Image_Proxy_URL").value = ithumbIMG.ThumbFilename
  149.  
  150.         rs.update
  151.         rs.Close
  152.         Set rs = Nothing
  153.             lstBox.items.insert lstBox.itemindex +1, itxtAdd.text
  154.             lstImgURL.items.insert lstImgURL.itemindex +1, ithumbIMG.PicFilename
  155.             lstImgProxyURL.items.insert lstImgProxyURL.itemindex +1, ithumbIMG.ThumbFilename
  156.         for i = 1 to lstBox.items.count
  157.             lstID.Items.Add(i)
  158.         Next
  159.  
  160.         call save
  161.  
  162.         if lstBox.items.count = 1 then
  163.                 lstBox.itemindex = 0
  164.                 lstID.itemindex = 0
  165.                 lstImgURL.itemindex = 0
  166.                 lstImgProxyURL.itemindex = 0
  167.                 position = lstBox.itemindex
  168.         else
  169.                 lstBox.itemindex = position +1
  170.                 lstID.itemindex = position +1
  171.                 lstImgURL.itemindex = position +1
  172.                 lstImgProxyURL.itemindex = position +1
  173.                 position = lstBox.itemindex
  174.         end if
  175.  
  176.         itxtAdd.clear
  177.         ithumbIMG.ThumbFilename = ""
  178.         ithumbIMG.PicFilename = ""
  179.         ithumbIMG.AssetUri = ""
  180.         ithumbIMG.Picture
  181.         cmdDel.Enabled = true
  182.  
  183.  
  184. call reorderButtons
  185. call fillAll
  186. End sub
  187. '-------------------------------------------------------------------------------
  188. ' DELETE FROM DATABASE
  189. '-------------------------------------------------------------------------------
  190. Sub dbDel(dbTable, selected)
  191.  
  192.         Set rs = CreateObject("ADODB.Recordset")
  193.         sql = "SELECT * FROM " & dbTable & " WHERE ID =" & selected
  194.         rs.Open sql, strDSN, adOpenDynamic, adLockOptimistic
  195.         With rs
  196.  
  197.                 If Not .BOF And Not .EOF Then
  198.                 .MoveLast
  199.                 .MoveFirst
  200.  
  201.                         If .Supports(adDelete) Then
  202.  
  203.                         .Delete
  204.  
  205.                         End If
  206.                 End If
  207.         End With
  208.         rs.Close
  209.         Set rs = Nothing
  210.  
  211. End sub
  212. '-------------------------------------------------------------------------------
  213. ' APPLY CHANGES TO DATABASE
  214. '-------------------------------------------------------------------------------
  215. Sub save
  216.  
  217. 'timer1.enabled = true
  218. 'lblSave.visible = true
  219.  
  220.         call writeMDB("tbl_Election", "ID", "Candidate", "Image_URL", "Image_Proxy_URL")
  221.         lstBox.clear
  222.         lstID.clear
  223.         lstImgURL.clear
  224.         lstImgProxyURL.clear
  225.  
  226.         call readMDB("tbl_Election", "ID", "Candidate", "Image_URL", "Image_Proxy_URL")
  227.  
  228.  
  229.  
  230. End sub
  231. '-------------------------------------------------------------------------------
  232.  
  233. '-------------------------------------------------------------------------------
  234. ' DELETE SELECTED RECORD FROM THE DATABASE
  235. '-------------------------------------------------------------------------------
  236. Sub cmdDelClick(Sender)
  237.  
  238.    call lastItem
  239.  
  240.         If lastItem = true then
  241.                 position = lstBox.itemindex -1
  242.         else
  243.                 position = lstBox.itemindex
  244.         end if
  245.  
  246.         sel = lstID.items(lstBox.itemindex)
  247.         call dbDel("tbl_Election",sel)
  248.  
  249.         lstBox.items.delete = lstBox.itemindex
  250.         lstID.items.delete = lstID.itemindex
  251.         lstImgURL.items.delete = lstImgURL.itemindex
  252.         lstImgProxyURL.items.delete = lstImgProxyURL.itemindex
  253.  
  254.         call save
  255.  
  256.         lstBox.itemindex = position
  257.         lstID.itemindex = position
  258.         lstImgURL.itemindex = position
  259.         lstImgProxyURL.itemindex = position
  260.  
  261. '   call emptyList
  262.   call reorderButtons
  263.    cmdAdd.enabled = false
  264.    itxtAdd.clear
  265.    call fillAll
  266. End Sub
  267.  
  268. '-------------------------------------------------------------------------------
  269. ' REORDER
  270. '-------------------------------------------------------------------------------
  271. Sub reorder(listboxArray, step, pos)
  272. step = Cint(step)
  273. pos = Cint(pos)
  274. position = listboxArray(1).itemindex
  275. for i = 0 to ubound(listboxArray)
  276.     listboxArray(i).items.insert listboxArray(i).itemindex + step, listboxArray(i).items(listboxArray(i).itemindex)
  277.     listboxArray(i).Items.delete = listboxArray(i).itemindex
  278. next
  279. call save
  280. for i = 0 to ubound(listboxArray)
  281.     listboxArray(i).itemindex = position + pos
  282. next
  283. position = listboxArray(1).itemindex
  284. 'call emptylist
  285. end sub
  286. '-------------------------------------------------------------------------------
  287. ' REORDER UP
  288. '-------------------------------------------------------------------------------
  289. Sub cmdUPClick(Sender)
  290.  
  291.         call firstItem
  292.         if firstItem = true then
  293.         else
  294.         call activeArray
  295.         call reorder(actArray,-1,-1)
  296.         end if
  297.    itxtAdd.clear
  298.    call fillAll
  299. End sub
  300. '-------------------------------------------------------------------------------
  301. ' REORDER DOWN
  302. '-------------------------------------------------------------------------------
  303. Sub cmdDOWNClick(Sender)
  304.  
  305.         call lastItem
  306.         if lastItem = true then
  307.         else
  308.         call activeArray
  309.         call reorder(actArray,+2,+1)
  310.         end if
  311.    itxtAdd.clear
  312.    call fillAll
  313. End sub
  314. '-------------------------------------------------------------------------------
  315. ' ACTIVE ARRAY OF LISTBOXES
  316. '-------------------------------------------------------------------------------
  317. sub activeArray
  318.         actArray = Array(lstID, lstBox, lstImgURL, lstImgProxyURL)
  319. end sub
  320. '-------------------------------------------------------------------------------
  321. ' FILL IN COMPONENTS
  322. '-------------------------------------------------------------------------------
  323. Function componentsToArray(keyword, count)
  324. TWUniListBox1.clear
  325. Dim components, component_name
  326.  
  327. Dim j
  328.   for i=0 to [_template].ComponentCount -1
  329.         component_name = [_template].Components(i).name
  330.         'msgbox [_template].Components(i).name
  331.        If StrComp(Left(component_name, count),keyword,1) = 0 Then
  332.        ' msgbox [_template].Components(i).name
  333.         myArray(j) =  [_template].Components(i)
  334.          j=j+1
  335.          TWUniListBox1.items.add([_template].Components(i).name)
  336.          end if
  337.  
  338.  
  339.   next
  340.  
  341. End function
  342. '-------------------------------------------------------------------------------
  343. Sub fillComponents(dbTable, prop)
  344.  
  345. Set objConnection = CreateObject("ADODB.Connection")
  346. Set objRecordSet = CreateObject("ADODB.Recordset")
  347.  
  348. objConnection.Open strDSN
  349.  
  350. objRecordSet.Open "SELECT * FROM " & dbTable & " ORDER BY ID ASC", _
  351.         objConnection, adOpenDynamic, adLockOptimistic
  352. With objRecordSet
  353.  
  354.     If Not .BOF And Not .EOF Then
  355.  
  356.    if lstBox.items.count > 0 then
  357.         objRecordSet.MoveFirst
  358.    end if
  359. Dim i
  360. i = 0
  361. Do Until objRecordSet.EOF
  362.     'msgbox MyArray(i).name
  363.  
  364.    if prop = "thumbfilename" then MyArray(i).thumbfilename = objRecordSet.Fields.Item("Image_Proxy_URL")
  365.    if prop = "text" then MyArray(i).text = objRecordSet.Fields.Item("Source_1")
  366.    if prop = "caption" then MyArray(i).caption = objRecordSet.Fields.Item("Candidate")
  367.  
  368.     i=i+1
  369.  
  370.     objRecordSet.MoveNext
  371. Loop
  372.    End if
  373. End With
  374. objRecordSet.Close
  375. objConnection.Close
  376.  
  377. End sub
  378.  
  379.  
  380.  
  381.  
  382.  
  383. Sub fillAll
  384.    call componentsToArray("thumb",5)
  385.    call fillComponents("tbl_Election", "thumbfilename")
  386.    call componentsToArray("txt",3)
  387.    call fillComponents("tbl_Election", "text")
  388.    call componentsToArray("lblname",7)
  389.    call fillComponents("tbl_Election", "caption")
  390. End sub
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement