記得Win TC可以看到程序占用內存數, VB也能嗎? 我的CPU還是Celeron(R) M CPU 430 @ 1.73G, 差吧? 來個例子吧! 見下貼;
怎樣優化以下代碼: Private Sub BOMAddCom_Click() Dim sql2 As String If Layout.Text = "" Then MsgBox "不良位置不能為空,請重新輸入!", 0, "提示!" Layout.SetFocus Exit Sub Else If MaterialSkuno.Text = "" Then MsgBox "物料料號不能為空,請重新輸入!", 0, "提示" MaterialSkuno.SetFocus Exit Sub Else If MaterialDescription.Text = "" Then MsgBox "物料描述不能為空,請重新輸入!", 0, "提示" MaterialDescription.SetFocus Exit Sub Else If StorageSpaces.Text = "" Then MsgBox "储位不能為空,請重新輸入!", 0, "提示" StorageSpaces.SetFocus Exit Sub Else sql2 = "select *from BOM where Name='" & UCase(NameCombox.Text) & "'and Layout='" & UCase(Layout.Text) & "'" Set rs = TransactSQL(sql2) If rs.EOF = False Then MsgBox "輸入位置已經存在,請重新輸入位置!", 0, "警告!" Layout.SetFocus Layout.Text = "" MaterialSkuno.Text = "" MaterialSkuno.Text = "" StorageSpaces.Text = "" Exit Sub Else sql2 = "insert into BOM (Name,Layout,MaterialSkuno,MaterialDescription,StorageSpaces) values ('" & UCase(NameCombox.Text) & "','" & Layout.Text & "','" & MaterialSkuno.Text & "','" & MaterialDescription.Text & "','" & StorageSpaces.Text & "')" TransactSQL (sql2) MsgBox "位置添加成功,請添加下一個位置", 0, "提示" Layout.SetFocus Layout.Text = "" MaterialSkuno.Text = "" MaterialDescription.Text = "" StorageSpaces.Text = "" Exit Sub End If Exit Sub End If Exit Sub End If Exit Sub End If Exit Sub End If End Sub
Private Sub BOMAddCom_Click() '功能說明: '1."Layout"設定; '2."MaterialSkuno"設定 '3."MaterialDescription"設定 '4."StorageSpaces"設定 Dim sql2 As String If Layout.Text = "" Then MsgBox "不良位置不能為空,請重新輸入!", 0, "提示!" Layout.SetFocus Exit Sub Else If MaterialSkuno.Text = "" Then MsgBox "物料料號不能為空,請重新輸入!", 0, "提示" MaterialSkuno.SetFocus Exit Sub Else If MaterialDescription.Text = "" Then MsgBox "物料描述不能為空,請重新輸入!", 0, "提示" MaterialDescription.SetFocus Exit Sub Else If StorageSpaces.Text = "" Then MsgBox "储位不能為空,請重新輸入!", 0, "提示" StorageSpaces.SetFocus Exit Sub Else sql2 = "select *from BOM where Name='" & UCase(NameCombox.Text) & "'and Layout='" & UCase(Layout.Text) & "'" Set rs = TransactSQL(sql2) If rs.EOF = False Then MsgBox "輸入位置已經存在,請重新輸入位置!", 0, "警告!" Layout.SetFocus Layout.Text = "" MaterialSkuno.Text = "" MaterialDescription.Text = "" StorageSpaces.Text = "" Exit Sub Else sql2 = "insert into BOM (Name,Skuno,Layout,MaterialSkuno,MaterialDescription,StorageSpaces,AddLayoutDateTime,AddLayoutUser) values ('" & UCase(NameCombox.Text) & "','" & NameSkuno & "','" & UCase(Layout.Text) & "','" & UCase(MaterialSkuno.Text) & "','" & MaterialDescription.Text & "','" & UCase(StorageSpaces.Text) & "','" & Now & "','" & loginUser & "')" TransactSQL (sql2) MsgBox "位置添加成功,請添加下一個位置", 0, "提示" Layout.SetFocus Layout.Text = "" MaterialSkuno.Text = "" MaterialDescription.Text = "" StorageSpaces.Text = "" Exit Sub End If Exit Sub End If Exit Sub End If Exit Sub End If Exit Sub End If End Sub
Private Sub BOMAddCom_Click() If Len(Layout.Text) = 0 Then MsgBox "不良位置不能為空,請重新輸入!", 0, "提示!" Layout.SetFocus Exit Sub End If
If Len(MaterialSkuno.Text) = 0 Then MsgBox "物料料號不能為空,請重新輸入!", 0, "提示" MaterialSkuno.SetFocus Exit Sub End If
If Len(MaterialDescription.Text) = 0 Then MsgBox "物料描述不能為空,請重新輸入!", 0, "提示" MaterialDescription.SetFocus Exit Sub End If
If Len(StorageSpaces.Text) = 0 Then MsgBox "储位不能為空,請重新輸入!", 0, "提示" StorageSpaces.SetFocus Exit Sub End If
Dim sql2 As String sql2 = "select top 1 1 from BOM where Name='" & UCase(NameCombox.Text) & "'and Layout='" & UCase(Layout.Text) & "'" Set rs = TransactSQL(sql2) If Not rs.EOF Then MsgBox "輸入位置已經存在,請重新輸入位置!", 0, "警告!" Layout.SetFocus Layout.Text = "" MaterialSkuno.Text = "" MaterialSkuno.Text = "" StorageSpaces.Text = "" Else sql2 = "insert into BOM (Name,Layout,MaterialSkuno,MaterialDescription,StorageSpaces) values ('" & UCase(NameCombox.Text) & "','" & Layout.Text & "','" & MaterialSkuno.Text & "','" & MaterialDescription.Text & "','" & StorageSpaces.Text & "')" TransactSQL (sql2) MsgBox "位置添加成功,請添加下一個位置", 0, "提示" Layout.SetFocus Layout.Text = "" MaterialSkuno.Text = "" MaterialDescription.Text = "" StorageSpaces.Text = "" End If end sub运行效率差别应该不会很明显。
2、优化代码,减少读写硬盘的频率。
1.簡化程序邏輯;
2.釋放不用資源;
3. 少用窗体,使用SSTab控件代替;
4.减少读写硬盘的频率;
還有嗎?
sql 查詢select,插入insert,更新update etc少用
看看你的循环体是否妥当,循环体内是否加了doevnets,
看看你的SQL语句是否妥当,这里用不好一条出库语句就可以等几分钟,
如果联网传输数据,看看网络连接是否正常,是否连续发送了较长的数据或连续接收了较长的数据。
VB也能嗎?
我的CPU還是Celeron(R) M CPU 430 @ 1.73G,
差吧?
來個例子吧!
見下貼;
Private Sub BOMAddCom_Click()
Dim sql2 As String
If Layout.Text = "" Then
MsgBox "不良位置不能為空,請重新輸入!", 0, "提示!"
Layout.SetFocus
Exit Sub
Else
If MaterialSkuno.Text = "" Then
MsgBox "物料料號不能為空,請重新輸入!", 0, "提示"
MaterialSkuno.SetFocus
Exit Sub
Else
If MaterialDescription.Text = "" Then
MsgBox "物料描述不能為空,請重新輸入!", 0, "提示"
MaterialDescription.SetFocus
Exit Sub
Else
If StorageSpaces.Text = "" Then
MsgBox "储位不能為空,請重新輸入!", 0, "提示"
StorageSpaces.SetFocus
Exit Sub
Else
sql2 = "select *from BOM where Name='" & UCase(NameCombox.Text) & "'and Layout='" & UCase(Layout.Text) & "'" Set rs = TransactSQL(sql2)
If rs.EOF = False Then
MsgBox "輸入位置已經存在,請重新輸入位置!", 0, "警告!"
Layout.SetFocus
Layout.Text = ""
MaterialSkuno.Text = ""
MaterialSkuno.Text = ""
StorageSpaces.Text = ""
Exit Sub
Else
sql2 = "insert into BOM (Name,Layout,MaterialSkuno,MaterialDescription,StorageSpaces) values ('" & UCase(NameCombox.Text) & "','" & Layout.Text & "','" & MaterialSkuno.Text & "','" & MaterialDescription.Text & "','" & StorageSpaces.Text & "')"
TransactSQL (sql2) MsgBox "位置添加成功,請添加下一個位置", 0, "提示"
Layout.SetFocus
Layout.Text = ""
MaterialSkuno.Text = ""
MaterialDescription.Text = ""
StorageSpaces.Text = ""
Exit Sub
End If
Exit Sub
End If
Exit Sub
End If
Exit Sub
End If
Exit Sub
End If
End Sub
'功能說明:
'1."Layout"設定;
'2."MaterialSkuno"設定
'3."MaterialDescription"設定
'4."StorageSpaces"設定
Dim sql2 As String
If Layout.Text = "" Then
MsgBox "不良位置不能為空,請重新輸入!", 0, "提示!"
Layout.SetFocus
Exit Sub
Else
If MaterialSkuno.Text = "" Then
MsgBox "物料料號不能為空,請重新輸入!", 0, "提示"
MaterialSkuno.SetFocus
Exit Sub
Else
If MaterialDescription.Text = "" Then
MsgBox "物料描述不能為空,請重新輸入!", 0, "提示"
MaterialDescription.SetFocus
Exit Sub
Else
If StorageSpaces.Text = "" Then
MsgBox "储位不能為空,請重新輸入!", 0, "提示"
StorageSpaces.SetFocus
Exit Sub
Else
sql2 = "select *from BOM where Name='" & UCase(NameCombox.Text) & "'and Layout='" & UCase(Layout.Text) & "'"
Set rs = TransactSQL(sql2)
If rs.EOF = False Then
MsgBox "輸入位置已經存在,請重新輸入位置!", 0, "警告!"
Layout.SetFocus
Layout.Text = ""
MaterialSkuno.Text = ""
MaterialDescription.Text = ""
StorageSpaces.Text = ""
Exit Sub
Else
sql2 = "insert into BOM (Name,Skuno,Layout,MaterialSkuno,MaterialDescription,StorageSpaces,AddLayoutDateTime,AddLayoutUser) values ('" & UCase(NameCombox.Text) & "','" & NameSkuno & "','" & UCase(Layout.Text) & "','" & UCase(MaterialSkuno.Text) & "','" & MaterialDescription.Text & "','" & UCase(StorageSpaces.Text) & "','" & Now & "','" & loginUser & "')"
TransactSQL (sql2)
MsgBox "位置添加成功,請添加下一個位置", 0, "提示"
Layout.SetFocus
Layout.Text = ""
MaterialSkuno.Text = ""
MaterialDescription.Text = ""
StorageSpaces.Text = ""
Exit Sub
End If
Exit Sub
End If
Exit Sub
End If
Exit Sub
End If
Exit Sub
End If
End Sub
Private Sub BOMAddCom_Click()
If Len(Layout.Text) = 0 Then
MsgBox "不良位置不能為空,請重新輸入!", 0, "提示!"
Layout.SetFocus
Exit Sub
End If
If Len(MaterialSkuno.Text) = 0 Then
MsgBox "物料料號不能為空,請重新輸入!", 0, "提示"
MaterialSkuno.SetFocus
Exit Sub
End If
If Len(MaterialDescription.Text) = 0 Then
MsgBox "物料描述不能為空,請重新輸入!", 0, "提示"
MaterialDescription.SetFocus
Exit Sub
End If
If Len(StorageSpaces.Text) = 0 Then
MsgBox "储位不能為空,請重新輸入!", 0, "提示"
StorageSpaces.SetFocus
Exit Sub
End If
Dim sql2 As String
sql2 = "select top 1 1 from BOM where Name='" & UCase(NameCombox.Text) & "'and Layout='" & UCase(Layout.Text) & "'"
Set rs = TransactSQL(sql2)
If Not rs.EOF Then
MsgBox "輸入位置已經存在,請重新輸入位置!", 0, "警告!"
Layout.SetFocus
Layout.Text = ""
MaterialSkuno.Text = ""
MaterialSkuno.Text = ""
StorageSpaces.Text = ""
Else
sql2 = "insert into BOM (Name,Layout,MaterialSkuno,MaterialDescription,StorageSpaces) values ('" & UCase(NameCombox.Text) & "','" & Layout.Text & "','" & MaterialSkuno.Text & "','" & MaterialDescription.Text & "','" & StorageSpaces.Text & "')"
TransactSQL (sql2)
MsgBox "位置添加成功,請添加下一個位置", 0, "提示"
Layout.SetFocus
Layout.Text = ""
MaterialSkuno.Text = ""
MaterialDescription.Text = ""
StorageSpaces.Text = ""
End If
end sub运行效率差别应该不会很明显。
1.簡化程序邏輯;
2.釋放不用資源;
3. 少用窗体,使用SSTab控件代替;
4.减少读写硬盘的频率;
5.多用數組及控件數組;應該可以結貼了吧,
謝謝各位;
没编译前用,在VB6IDE环境中运行程序,然后Ctrl+Alt+Del看进程中VB6.EXE所占内存和CPU占用情况,可以大体了解你的程序的运行情况。编译后可找你编译的可执行文件名。从你提供的程序看不出什么大问题,问题可能不是出在这里。没看到你写内存数,如果是256内存,装XP系统,再装点别的什么,跑起来本身就会比较累不过看你的CPU为1.73G 想你的内存应该不少于512M建议楼主在不启动VB6的情况下,看看剩余可用内存是多少?CPU占用率是多少?(可在任务管理器中的性能中观测)我的本子在没启VB6的情况下内存已经占用800多M了,不过它是2G内存。