帮忙看看代码怎么优化?
几个datagrid用了同样的columns,能否简化成函数调用格式?我试验了一下,好像不支持project作为函数参数调用,求高手指点!Adodc1.ConnectionString = Conn
Adodc1.RecordSource = "select system,belong,category,number,name,revision,correspondence,schedule_date,actual_date,commodity_code,commodity_name,note from ni_doc where system='" & sys & "' and belong = 'Document Received' and category='P&ID'"
Adodc1.Refresh
Set DataGrid1.DataSource = Adodc1
With DataGrid1
.Columns(0).Width = 400
.Columns(1).Width = 0
.Columns(2).Width = 1300
.Columns(3).Width = 1800
.Columns(4).Width = 2800
.Columns(5).Width = 500
.Columns(6).Width = 1000
.Columns(7).Width = 1000
.Columns(8).Width = 1000
.Columns(9).Width = 1000
.Columns(10).Width = 2000
.Columns(11).Width = 4000
End With
Adodc2.ConnectionString = Conn
Adodc2.RecordSource = "select system,belong,category,number,name,revision,correspondence,schedule_date,actual_date,commodity_code,commodity_name,note from ni_doc where system='" & sys & "' and belong = 'Document Received' and category='SSD'"
Adodc2.Refresh
Set DataGrid2.DataSource = Adodc2
With DataGrid2
.Columns(0).Width = 400
.Columns(1).Width = 0
.Columns(2).Width = 1300
.Columns(3).Width = 1800
.Columns(4).Width = 2800
.Columns(5).Width = 500
.Columns(6).Width = 1000
.Columns(7).Width = 1000
.Columns(8).Width = 1000
.Columns(9).Width = 1000
.Columns(10).Width = 2000
.Columns(11).Width = 4000
End With
'test specification
Adodc3.ConnectionString = Conn
Adodc3.RecordSource = "select system,belong,category,number,name,revision,correspondence,schedule_date,actual_date,commodity_code,commodity_name,note from ni_doc where system='" & sys & "' and belong = 'Document Received' and category='Test Specification'"
Adodc3.Refresh
Set DataGrid3.DataSource = Adodc3
With DataGrid3
.Columns(0).Width = 400
.Columns(1).Width = 0
.Columns(2).Width = 1300
.Columns(3).Width = 1800
.Columns(4).Width = 2800
.Columns(5).Width = 500
.Columns(6).Width = 1000
.Columns(7).Width = 1000
.Columns(8).Width = 1000
.Columns(9).Width = 1000
.Columns(10).Width = 2000
.Columns(11).Width = 4000
End With
'test procedure
Adodc4.ConnectionString = Conn
Adodc4.RecordSource = "select system,belong,category,number,name,revision,correspondence,schedule_date,actual_date,commodity_code,commodity_name,note from ni_doc where system='" & sys & "' and belong = 'Document Received' and category='Test Procedure'"
Adodc4.Refresh
Set DataGrid4.DataSource = Adodc4
With DataGrid4
.Columns(0).Width = 400
.Columns(1).Width = 0
.Columns(2).Width = 1300
.Columns(3).Width = 1800
.Columns(4).Width = 2800
.Columns(5).Width = 500
.Columns(6).Width = 1000
.Columns(7).Width = 1000
.Columns(8).Width = 1000
.Columns(9).Width = 1000
.Columns(10).Width = 2000
.Columns(11).Width = 4000
End With
'Vendor Manual
Adodc5.ConnectionString = Conn
Adodc5.RecordSource = "select system,belong,category,number,name,revision,correspondence,schedule_date,actual_date,commodity_code,commodity_name,note from ni_doc where system='" & sys & "' and belong = 'Document Received' and category='Vendor Manual'"
Adodc5.Refresh
Set DataGrid5.DataSource = Adodc5
With DataGrid5
.Columns(0).Width = 400
.Columns(1).Width = 0
.Columns(2).Width = 1300
.Columns(3).Width = 1800
.Columns(4).Width = 2800
.Columns(5).Width = 500
.Columns(6).Width = 1000
.Columns(7).Width = 1000
.Columns(8).Width = 1000
.Columns(9).Width = 1000
.Columns(10).Width = 2000
.Columns(11).Width = 4000
End With
'Other Document
Adodc6.ConnectionString = Conn
Adodc6.RecordSource = "select system,belong,category,number,name,revision,correspondence,schedule_date,actual_date,commodity_code,commodity_name,note from ni_doc where system='" & sys & "' and belong = 'Document Received' and category='Other Document'"
Adodc6.Refresh
Set DataGrid6.DataSource = Adodc6
With DataGrid6
.Columns(0).Width = 400
.Columns(1).Width = 0
.Columns(2).Width = 1300
.Columns(3).Width = 1800
.Columns(4).Width = 2800
.Columns(5).Width = 500
.Columns(6).Width = 1000
.Columns(7).Width = 1000
.Columns(8).Width = 1000
.Columns(9).Width = 1000
.Columns(10).Width = 2000
.Columns(11).Width = 4000
End With --------------------编程问答-------------------- --------------------编程问答-------------------- 自己做一个控件,可以让用户自由设置要显示的列和设置列的宽度 --------------------编程问答-------------------- 我的不需要自由设置,就是上面的固定值。
貌似VB不支持object做函数参数传递
比如
public sub setdatagrid (mygrid as datagrid)
dim mygrid as datagrid
with mygrid
.Columns(0).Width = 400
.Columns(1).Width = 0
.Columns(2).Width = 1300
.Columns(3).Width = 1800
.Columns(4).Width = 2800
.Columns(5).Width = 500
.Columns(6).Width = 1000
.Columns(7).Width = 1000
.Columns(8).Width = 1000
.Columns(9).Width = 1000
.Columns(10).Width = 2000
.Columns(11).Width = 4000
End With
end sub
setdatagrid (datagrid1)
在调用这个函数的时候会出错/而且系统直接崩溃,无法调试 --------------------编程问答--------------------
--------------------编程问答--------------------
public sub setdatagrid (mygrid as datagrid)
'去掉dim mygrid as datagrid
with mygrid
.Columns(0).Width = 400
.Columns(1).Width = 0
.Columns(2).Width = 1300
.Columns(3).Width = 1800
.Columns(4).Width = 2800
.Columns(5).Width = 500
.Columns(6).Width = 1000
.Columns(7).Width = 1000
.Columns(8).Width = 1000
.Columns(9).Width = 1000
.Columns(10).Width = 2000
.Columns(11).Width = 4000
End With
end sub
right --------------------编程问答-------------------- 按上面的方法改过之后直接运行没有问题,但编译通不过,提示“未定义"错误。高手给个提示,谢谢! --------------------编程问答-------------------- 未定义就是有标识符没有声明,能直接运行只能说明被运行的代码是没错误的,VB并不检查其它暂没运行的代码;而编译时是要检查全部代码的,也就是说你的暂没运行的代码有错误。再说编译器会把光标停在你错误的代码处,找出问题没这么难吧! --------------------编程问答-------------------- 按F5运行, 看错在那哪一句...... --------------------编程问答-------------------- public sub setdatagrid (mygrid as Object)
with mygrid
.Columns(0).Width = 400
.Columns(1).Width = 0
.Columns(2).Width = 1300
.Columns(3).Width = 1800
.Columns(4).Width = 2800
.Columns(5).Width = 500
.Columns(6).Width = 1000
.Columns(7).Width = 1000
.Columns(8).Width = 1000
.Columns(9).Width = 1000
.Columns(10).Width = 2000
.Columns(11).Width = 4000
End With
end sub
setdatagrid datagrid1
补充:VB , 控件