# Re: Answer "如何在run time 改变DBGRID 的datasource"

Posted by Spencer Yang on April 21, 1999 at 15:37:19:

In Reply to: 如何在run time 改变DBGRID 的datasource posted by Karrie on April 21, 1999 at 12:41:50:

1)先不论你的要求为何，你的做法并非资料库管理方式，假如一年365年计，预计使用寿命为10年，你至少有3650个的table,除非你是ERP专业开发厂商，号称有5000余TABLE(SAP),否则一个TABLE即可。
2)假如你认为每日一个TABLE是必须, 如何连结呢，本人所写的范例请参考(本人从邮政总局DOWNLOAD其路名资料库，每月邮递区号为一个TABLE,计400余TABLE)
3)本人系使用ADO 配合 DATAGRID, 与DATA搭配DBGRID不同，不过可以参考"RecordSource"部分。

Dim blndblPrefClick As Boolean ' 是否单击变数
Dim intStatus As Integer ' 0: 一般程序; 1: 依邮递区号数值查询
Dim strPref As String ' 路首字查询参数
Dim strRoad As String
Dim strRoadNo As String ' 道路编号变数
Dim strZIP As String ' 邮递区号变数
Dim strConnectionString As String ' 连接字串变数
Dim strSQL As String ' SQL 字串变数
'------------------------------------------------
' A0 将下列从表单模组变数传递给下列变数
'------------------------------------------------
blndblPrefClick = mblndblPrefClick
intStatus = mintStatus
strPref = mstrPref
strZIP = mstrZIP
'------------------------------------------------
' 检查路首字栏位是否点选
'------------------------------------------------
If blndblPrefClick = False Then
MsgBox "路首字栏位未选！", vbExclamation + vbOKOnly, "函证系统"
Exit Sub
End If
'------------------------------------------------
' D0 资料控制项
'------------------------------------------------
strConnectionString = "Provider=Microsoft.Jet.OLEDB.3.51;Persist Security Info=False;Data Source=D:\Utility\utlStandard.mdb"
Select Case strZIP
Case 100, 103, 104, 105, 106, 108, 110, 111, 112, 114, 115
strZIP = "100"
Case 200, 201, 202, 203, 204, 205, 206
strZIP = "200"
Case 400, 401, 402, 403, 404, 406, 407, 408
strZIP = "400"
Case 700, 701, 702, 703, 704, 708, 709
strZIP = "700"
Case 800, 801, 802, 803, 804, 805, 806, 807, 811, 812, 813
strZIP = "800"
End Select
strRoad = Trim("R" & strZIP & "A")
strRoadNo = txtRoadNo.Text
Select Case intStatus ' 0: 一般程序; 1: 依道路编号数值查询
Case 0
strSQL = "SELECT MID(ROAD,1,1) AS PREF, ROAD, ROAD_NO, RKEY " _
& "From " & strRoad _
& " WHERE MID(ROAD,1,1) =" & Chr(39) & strPref & Chr(39) _
& " ORDER BY ROAD_NO "
Case 1
strSQL = "SELECT MID(ROAD,1,1) AS PREF, ROAD, ROAD_NO, RKEY " _
& "From " & strRoadNo _
& " WHERE MID(ROAD,1,1) =" & Chr(39) & strPref & Chr(39)
End Select
With adoRoad
.ConnectionString = strConnectionString
.CommandType = adCmdText
.RecordSource = strSQL
.Refresh
End With
End Sub

Follow Ups: