本帖最后由 xyhqqaa 于 2017-9-19 08:14 编辑
新手请教哥哥们:如图所示,本人希望选择甲乙丙公司的时候,combo2自动加载数据库内对应的部门类别出来,但是目前发现一个情况,运行代码后,数据库却没法快速连接查询,
①.导致查询,listview显示的公司名称跟combo1上的公司名称不一致
②.需要多次选择combo里面公司类别后,才会查询数据库。
新手求指教,如何优化代码更简洁比较好呢#include <ButtonConstants.au3>
#include <ComboConstants.au3>
#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>
#include <WindowsConstants.au3>
#include <GuiListView.au3>
Local $sqlfind,$read1,$read2
#Region ### START Koda GUI section ### Form=
$Form1 = GUICreate("Form1", 513, 392, 521, 299)
$Combo1 = GUICtrlCreateCombo("", 24, 296, 145, 25, BitOR($CBS_DROPDOWN,$CBS_AUTOHSCROLL))
GUICtrlSetData($Combo1,"甲公司|乙公司|丙公司")
$Combo2 = GUICtrlCreateCombo("", 24, 336, 145, 25, BitOR($CBS_DROPDOWN,$CBS_AUTOHSCROLL))
$ListView1 = GUICtrlCreateListView("", 8, 8, 498, 262)
_GUICtrlListView_AddColumn($ListView1, "编号", 45)
_GUICtrlListView_AddColumn($ListView1, "公司", 150)
_GUICtrlListView_AddColumn($ListView1, "部门", 120)
GUISetState(@SW_SHOW)
#EndRegion ### END Koda GUI section ###
$mdb_data_path = @ScriptDir & "\data.mdb"
$mdb_data_pwd=""
$T = "*"
$tblname = "部门"
While 1
$nMsg = GUIGetMsg()
Switch $nMsg
Case $GUI_EVENT_CLOSE
Exit
Case $Combo1
GUICtrlSetData($Combo2,"")
$sqlfind = "Select " & $T & " From " & $tblname & " where " & " 公司" & "= '" & $read1 & "'"
chaxun()
EndSwitch
WEnd
Func chaxun();查询
$read1 = GUICtrlRead($Combo1)
$read2 = GUICtrlRead($Combo2)
GUICtrlSendMsg($ListView1, $LVM_DELETEALLITEMS, 0, 0)
$addfld = ObjCreate("ADODB.Connection")
$addfld.Open("Provider=Microsoft.Jet.OLEDB.4.0; Data Source=" & $mdb_data_path & ";Jet Oledb:Database Password=" & $mdb_data_pwd)
$RS = ObjCreate("ADODB.Recordset")
$RS.ActiveConnection = $addfld
$RS.Open($sqlfind)
While Not $RS.eof And Not $RS.bof
If @error = 1 Then ExitLoop
GUICtrlCreateListViewItem($RS.Fields(0).value & "|" & $RS.Fields(1).value & "|" & $RS.Fields(2).value , $ListView1)
$a=$RS.Fields(2).value
GUICtrlSetData($Combo2,$a)
$RS.movenext
WEnd
$RS.close
$addfld.Close
EndFunc ;==>chaxun
|