雷迪姆阵列???
当我有 Preserve 关键字时,我不断收到错误,当我将其删除时,我不会收到错误...我需要保留数组中的数据,因此,如何同时保留并使数组变大。
帮助中的注释说您只能在使用保留时更改最后一个维度,但我不明白这意味着什么......
我的数组设置如下
Public roughPart() 作为字符串
Public roughPartQty() 作为整数
Public topoutPart() 作为字符串
Public topoutPartQty() 作为整数
这是与上述问题相关的代码部分...
Else:
Set blkPart = TempSet(X)
If blkPart.Name = "Assembly" Then
cntAssembly = cntAssembly + 1
For Each attPart In blkPart.GetAttributes
Select Case attPart.TagString
Case "ASSEMBLY":
If attPart.TextString = "" Then
Exit For
Else:
Assembly = attPart.TextString
End If
End Select
Next attPart
'SQLstring = "Select PartID, QTY, AssemblyType From tblAssembly Where AssemblyName ='" & Assembly & "';"
SQLstring = "Select tblAssembly.PartID, tblAssembly.Quantity, tblAssembly.AssemblyType, tblParts.PartName From tblAssembly Inner Join tblParts ON tblAssembly.PartID = tblParts.PartID Where tblAssembly.AssemblyName ='" & Assembly & "';"
Set PlumbingREC = PlumbingDB.OpenRecordset(SQLstring, dbOpenDynaset)
If PlumbingREC.RecordCount > 0 Then
If PlumbingREC.EOF And PlumbingREC.BOF Then
Exit Sub
Else:
PlumbingREC.MoveFirst
Y = 0
Do While Not PlumbingREC.EOF
ReDim roughPart(Y, cntAssembly) As String
ReDim roughPartQty(Y, cntAssembly) As Integer
If PlumbingREC.Fields("AssemblyType").value = "Rough" Then
roughPart(Y, cntAssembly) = PlumbingREC.Fields("PartName").value
roughPartQty(Y, cntAssembly) = PlumbingREC.Fields("Quantity").value
Y = Y + 1
End If
PlumbingREC.MoveNext
Loop
PlumbingREC.MoveFirst
Y = 0
Do While Not PlumbingREC.EOF
ReDim topoutPart(Y, cntAssembly) As String
ReDim topoutPartQty(Y, cntAssembly) As Integer
If PlumbingREC.Fields("AssemblyType").value = "TopOut" Then
topoutPart(Y, cntAssembly) = PlumbingREC.Fields("PartName").value
topoutPartQty(Y, cntAssembly) = PlumbingREC.Fields("Quantity").value
Y = Y + 1
End If
PlumbingREC.MoveNext
Loop
End If
End If
PlumbingREC.Close
Set PlumbingREC = Nothing
我正在尝试执行的操作的摘要:
我有1个块,“程序集”,其中1个属性“程序集”。 该属性的值是存储在 *.mdb 文件中的程序集名称。 因此,我的部件存储在数据库中,而不是存储在块中的多个属性中。
一个组件块可能具有仅引用一个或两个零件的装配体名称,但另一个模块可能具有 20 到 30 个零件。 因此,当我创建(redim)我的数组时...roughPart(X,Y) 第一列中可能只有 2 行数据,然后当查询下一个程序集块并且它有 30 个零件时,该列中将有 30 行。 这是一个问题吗??
提前感谢您的帮助...我希望这足够清楚,如果你需要,我会发布更多细节......
谢谢
**** Hidden Message ***** 出于某种原因,我认为您只能重新划分一维数组。
这意味着一旦您将数组的尺寸设置为其初始大小,如下所示:
ReDim粗糙部分(Y, cntAssembly)作为循环中的字符串
,每次后续迭代只能替换cntAssembly的新值。Y的值不能改变。
实现目标的一种方法是预先计算记录计数,以便在开始循环之前建立每个数组的第一个维度。 此时,我正在考虑调用一个过程,该过程将获取找到的数据并立即将其导出到Excel工作簿。 而不是查询所有数据并编译成数百个变量,然后将这些变量发送到电子表格。 我认为这将消除对我的阵列的需求...
你同意吗? 或者我错过了什么?
顺便说一句..
当我再次阅读ReDim的帮助时。它更有意义..
您只能更改最后一个维度的大小,而根本无法更改维度的数量。
因此,如果您从单个开始,则无法更改为2 dim数组。 但是,如果你有一个暗淡,你可以随时更改该暗淡的大小,如果你有一个多重暗淡的数组,你只能改变最后一个暗淡的大小(第2个表示双调,第3个表示3层,依此类推)。
这意味着,如果我想使用此方法,我必须将我的第一个 Dim(Rows)预设为足够大的 # 以容纳所有可能的零件,然后每当找到新组件时,我就可以更改第 2 个 dim(列)的 # 。
我认为立即将数据导出到excel比先将数据编译为数组要容易得多。 我会这样做,除非你们中的一个人有理由我不应该这样做。
再次感谢您的帮助... 谢谢你,查克
我预设它的问题有两个方面:
1。当我使用我的SQLstring查询我的数据库时,PlumbingREC的记录计数总是1。这是因为我在进行内部联接吗?
2.另一个问题是,查询的第一个块可能需要数组的第一个Dim为5个条目,而下一个块则可能需要30个条目。因此,在我看来,我必须对所有程序集进行记录计数,然后重新循环程序集,以将零件应用于数组
我完全同意立即导出到Excell,只要不需要重新处理数据(组合排序和过滤东西)
创建交错数组的解决方法是在这样的数组中使用集合类型:
Sub test()
Dim Cl() As New Collection
ReDim Preserve Cl(2)
Cl(0).Add ("YourStuff0.1")
Cl(0).Add ("YourStuff0.2")
Cl(0).Add ("YourStuff0.3")
Cl(0).Add ("YourStuff0.4")
Cl(1).Add ("YourStuff1.1")
Cl(1).Add ("YourStuff1.2")
Cl(1).Add ("YourStuff1.3")
Cl(1).Add ("YourStuff1.4")
Cl(2).Add ("YourStuff2.1")
ReDim Preserve Cl(3)
Cl(3).Add ("YourStuff3.1")
Cl(3).Add ("YourStuff3.2")
Cl(3).Add ("YourStuff3.3")
Cl(3).Add ("YourStuff3.4")
End Sub
你可以通过这种方式添加两个维度。缺点是,与真数组相比,收集对象的速度很慢。
因此,如果您不需要将数据保存在内存中...别这样。
您是否移动到最后一条记录(. movelast),因为在所有记录真正加载到记录集中之前,计数不会显示实际大小
也许我弄错了您想要什么,但是如果您知道记录的数量和字段的数量(Recordset.Fields.Count),您可以重新设置一个数组。
另一方面,如果您已经在记录集中保存了数据,我认为没有必要在没有任何特殊原因的情况下将其传输到数组中。 Kevin,尝试使用GetRow方法:
这是我的程序代码片段:
Private Sub UserForm_Initialize()
On Error GoTo ErrorHandler
Dim Cnxn As ADODB.Connection
Dim rstNew As ADODB.Recordset
Dim strCnxn As String
Dim strSQL As String
Dim retArr As Variant
Dim i As Long, j As Long
' Open connection
strCnxn = "Provider=Microsoft.Jet.OLEDB.4.0;" & _
"Data Source=D:\AUTOLISP\LISPS\VBA\ACCESS\data.mdb;"
Set Cnxn = New ADODB.Connection
Cnxn.Open strCnxn
' Open table
Set rstNew = New ADODB.Recordset
strSQL = "prot_Viabilita" ' table name
rstNew.Open strSQL, Cnxn, adOpenKeyset, adLockOptimistic, adCmdTable
retArr = rstNew.GetRows
Me.Caption = "Data from table " & Chr(34) & strSQL & Chr(34)
'
' Transpose record data and populate list box
'
ReDim dataArr(UBound(retArr, 2), UBound(retArr, 1)) As String
For i = 0 To UBound(retArr, 1)
For j = 0 To UBound(retArr, 2)
dataArr(j, i) = CStr(retArr(i, j))
Next
Next
ListBox1.List() = dataArr
' clean up
rstNew.Close
Cnxn.Close
Set rstNew = Nothing
Set Cnxn = Nothing
Exit Sub
ErrorHandler:
' clean up
If Not rstNew Is Nothing Then
If rstNew.State = adStateOpen Then rstNew.Close
End If
Set rstNew = Nothing
If Not Cnxn Is Nothing Then
If Cnxn.State = adStateOpen Then Cnxn.Close
End If
Set Cnxn = Nothing
If Err0 Then
MsgBox Err.Source & "-->" & Err.Description, , "Error"
End If
End Sub
>'J'< 能不能把y和cntassembly变量调换一下,让y是最后一个? 晚了一天,少了一分钱。
页:
[1]
2