VBA问题,高手来帮我改进一下

VBA问题,高手来帮我改进一下,第1张

IfThenElse 语句
根据表达式的值有条件地执行一组语句。语法
If condition Then [statements][Else elsestatements]
例:If A > 10 Then A = A + 1 : B = B + A : C = C + B
或者,可以使用块形式的语法:
If condition Then
[statements]
[ElseIf condition-n Then
[elseifstatements]
[Else
[elsestatements]]
End If
例:If A > 10 Then
A = A + 1
B = B + A
C = C + B
endif
Sub 按钮8_单击()
Dim cnn As New ADODBConnection, rs As New ADODBRecordset
Dim i As Long, mydata As String, myTable As String, SQL As String
Dim abc As String
mydata = "BPW_Datamarts"
myTable = "经销商销售200803"
abc = ActiveSheetCells(6, 6)Value
With cnn
ConnectionString = "Driver={SQL Server};Server=1030129;Database=BPW_Datamarts;Uid=De56;Trusted_Connection=Yes;"
Open
End With
SQL = "select 订单号,产品编号,产品名称,发票数量,零售单价,零售金额,实售单价,实售金额,客户编号,客户简称,发票日期 from " & myTable & " where 发货单号=" & "'" & abc & "'"
rsOpen SQL, cnn, adOpenKeyset, adLockOptimistic
If rsRecordCount = 0 Then MsgBox "该发货单不存在"
i = 9
Set sht = ThisWorkbookWorksheets("单据-按订单号查询")
shtCells(6, 2) = rsFields("客户编号")
shtCells(6, 3) = rsFields("客户简称")
shtCells(6, 9) = rsFields("发票日期")
Do While Not rsEOF
shtCells(i, 1) = rsFields("订单号")
shtCells(i, 2) = rsFields("产品编号")
shtCells(i, 3) = rsFields("产品名称")
shtCells(i, 4) = rsFields("发票数量")
shtCells(i, 5) = rsFields("零售单价")
shtCells(i, 6) = rsFields("零售金额")
shtCells(i, 7) = rsFields("实售单价")
shtCells(i, 8) = rsFields("实售金额")
rsMoveNext
i = i + 1
Loop
rsClose
End Sub

'初始化连接数据库
Sub Init()
If ConnState <> 0 Then ConnClose '检查数据连接是否开着,如果开着就关闭当前连接
Dim conStr As String
conStr = "Provider=MicrosoftJetOLEDB40;Data Source=" & ThisWorkbookPath & "\db1mdb;Persist Security Info=False" '打开当前EXCEL工作薄相同目录下的db1mdb数据库
ConnOpen conStr '连接上conStr指定的数据库
End Sub
RsOpen "select from ygxx where 姓名='" & TextBox1 & "'", Conn, 1, 1 '把ygxx表中“姓名”字段内容等于TextBox1中内容的数据检索出来
RsOpen "select from ygxx", Conn, 1, 3 '把ygxx表中的全部数据检索出来
Rs(1)应该是Recordset的第一个字段


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zz/13474332.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-08-14
下一篇 2023-08-14

发表评论

登录后才能评论

评论列表(0条)

保存