| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 349 人关注过本帖
标题:如何简化优化代码
只看楼主 加入收藏
yuk_yu
Rank: 3Rank: 3
等 级:论坛游侠
帖 子:334
专家分:134
注 册:2009-3-16
结帖率:85.71%
收藏
已结贴  问题点数:20 回复次数:4 
如何简化优化代码
程序代码:
    If Me.Combo1(0) <> "" And Me.Combo1(1) <> "" And Me.Combo1(2) <> "" And Me.Combo1(3) <> "" Then
        SQL = "select * from DataList"
        SQL = SQL + " where [UploadSN] ='" & Me.Combo1(0) & "' and [BuyerName] ='" & Me.Combo1(1) & "' and [MaterialPart] ='" & Me.Combo1(2) & "' and [YesOrNo] ='" & Me.Combo1(3) & "'"

    ElseIf Me.Combo1(0) <> "" And Me.Combo1(1) = "" And Me.Combo1(2) = "" And Me.Combo1(3) = "" Then
        SQL = "select * from DataList where [UploadSN] ='" & Me.Combo1(0) & "' "

    ElseIf Me.Combo1(0) <> "" And Me.Combo1(1) <> "" And Me.Combo1(2) = "" And Me.Combo1(3) = "" Then
        SQL = "select * from DataList where [UploadSN] ='" & Me.Combo1(0) & "' and [BuyerName] ='" & Me.Combo1(1) & "' "

    ElseIf Me.Combo1(0) <> "" And Me.Combo1(1) <> "" And Me.Combo1(2) <> "" And Me.Combo1(3) = "" Then
        SQL = "select * from DataList where [UploadSN] ='" & Me.Combo1(0) & "' and [BuyerName] ='" & Me.Combo1(1) & "' and [MaterialPart] ='" & Me.Combo1(2) & "' "

    ElseIf Me.Combo1(0) = "" And Me.Combo1(1) = "" And Me.Combo1(2) = "" And Me.Combo1(3) <> "" Then
        SQL = "select * from DataList where [YesOrNo] ='" & Me.Combo1(3) & "'"

    ElseIf Me.Combo1(0) = "" And Me.Combo1(1) = "" And Me.Combo1(2) <> "" And Me.Combo1(3) <> "" Then
        SQL = "select * from DataList where [MaterialPart] ='" & Me.Combo1(2) & "' and [YesOrNo] ='" & Me.Combo1(3) & "'"

    ElseIf Me.Combo1(0) = "" And Me.Combo1(1) <> "" And Me.Combo1(2) <> "" And Me.Combo1(3) <> "" Then
        SQL = "select * from DataList where [BuyerName] ='" & Me.Combo1(1) & "' and [MaterialPart] ='" & Me.Combo1(2) & "' and [YesOrNo] ='" & Me.Combo1(3) & "'"

    ElseIf Me.Combo1(0) <> "" And Me.Combo1(1) = "" And Me.Combo1(2) <> "" And Me.Combo1(3) = "" Then
        SQL = "select * from DataList"
        SQL = SQL + " where [UploadSN] ='" & Me.Combo1(0) & "' and [MaterialPart] ='" & Me.Combo1(2) & "'"
    ElseIf Me.Combo1(0) = "" And Me.Combo1(1) = "" And Me.Combo1(2) <> "" And Me.Combo1(3) = "" Then
        SQL = "select * from DataList"
        SQL = SQL + " where [MaterialPart] ='" & Me.Combo1(2) & "'"

    ElseIf Me.Combo1(0) <> "" And Me.Combo1(1) = "" And Me.Combo1(2) = "" And Me.Combo1(3) <> "" Then
        SQL = "select * from DataList"
        If Me.Combo1(3).Text = "Null" Then
            SQL = SQL + " where [UploadSN] ='" & Me.Combo1(0) & "' and [YesOrNo] is null"
        Else
            SQL = SQL + " where [UploadSN] ='" & Me.Combo1(0) & "' and [YesOrNo] ='" & Me.Combo1(3) & "'"
        End If

    ElseIf Me.Combo1(0) = "" And Me.Combo1(1) = "" And Me.Combo1(2) = "" And Me.Combo1(3) = "" Then
        MsgBox "Pls selct one condition", vbCritical, "Caution"
        Exit Sub
    End If
搜索更多相关主题的帖子: 如何 
2014-03-25 17:04
lowxiong
Rank: 12Rank: 12Rank: 12
等 级:贵宾
威 望:27
帖 子:652
专家分:3402
注 册:2008-5-7
收藏
得分:20 
dim a as string,i as integer,SQL as string
SQL = "select * from DataList "
a=""
for i=0 to 3
  combo1(i)=trim(combo1(i))
  a=a & combo1(i)
next
if a<>"" then
  sql=sql & " where "
  if combo1(0)<>"" then sql=sql & "[UploadSN] ='" & (0) & "' and "
  if combo1(1)<>"" then sql=sql & "[BuyerName] ='" & (1) & "' and "
  if combo1(2)<>"" then sql=sql & "[MaterialPart] ='" & (2) & "' and "
  if combo1(3)<>"" then sql=sql & "[YesOrNo] ='" & (3) & "' "
  if right(sql,4)="and " then sql=left(sql,len(sql)-4)
else
  MsgBox "Pls selct one condition", vbCritical, "Caution"
  Exit Sub
End If
   




[ 本帖最后由 lowxiong 于 2014-3-25 17:39 编辑 ]
2014-03-25 17:38
yuk_yu
Rank: 3Rank: 3
等 级:论坛游侠
帖 子:334
专家分:134
注 册:2009-3-16
收藏
得分:0 
回复 2楼 lowxiong
谢谢版主
2014-03-25 18:44
风吹过b
Rank: 20Rank: 20Rank: 20Rank: 20Rank: 20
等 级:贵宾
威 望:364
帖 子:4947
专家分:30084
注 册:2008-10-15
收藏
得分:0 
dim a as string,i as integer,SQL as string

SQL = "select * from DataList "

'如果存在,则拼上条件去
  if combo1(0)<>"" then a=a  & "and [UploadSN] ='" & (0) & "'"
  if combo1(1)<>"" then a=a  & "and [BuyerName] ='" & (1) & "'"
  if combo1(2)<>"" then a=a  & "and [MaterialPart] ='" & (2) & "'"
  if combo1(3)<>"" then a=a  & "and [YesOrNo] ='" & (3) & "' "

'检查拼起来的条件,如果有内容,则生成 SQL 命令
if len(a)>0 then
  sql=sql & " where " & mid(a,4)            '如果有内容,那么开头 and 一定是多余的,直接去掉就可了。
else
  MsgBox "Pls selct one condition", vbCritical, "Caution"
  Exit Sub
End If

授人于鱼,不如授人于渔
早已停用QQ了
2014-03-26 08:53
yuk_yu
Rank: 3Rank: 3
等 级:论坛游侠
帖 子:334
专家分:134
注 册:2009-3-16
收藏
得分:0 
回复 4楼 风吹过b
谢谢斑竹,太厉害了,学习ing...
2014-03-26 15:07
快速回复:如何简化优化代码
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.024643 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved