乐筑天下

搜索
欢迎各位开发者和用户入驻本平台 尊重版权,从我做起,拒绝盗版,拒绝倒卖 签到、发布资源、邀请好友注册,可以获得银币 请注意保管好自己的密码,避免账户资金被盗
查看: 120|回复: 10

雷迪姆阵列???

[复制链接]

18

主题

55

帖子

1

银币

初露锋芒

Rank: 3Rank: 3Rank: 3

铜币
127
发表于 2006-11-16 13:17:46 | 显示全部楼层 |阅读模式
当我有 Preserve 关键字时,我不断收到错误,当我将其删除时,我不会收到错误...
我需要保留数组中的数据,因此,如何同时保留并使数组变大。
帮助中的注释说您只能在使用保留时更改最后一个维度,但我不明白这意味着什么......
我的数组设置如下
Public roughPart() 作为字符串
Public roughPartQty() 作为整数
Public topoutPart() 作为字符串
Public topoutPartQty() 作为整数
这是与上述问题相关的代码部分...
  1. Else:
  2.         Set blkPart = TempSet(X)
  3.             If blkPart.Name = "Assembly" Then
  4.                 cntAssembly = cntAssembly + 1
  5.                 For Each attPart In blkPart.GetAttributes
  6.                     Select Case attPart.TagString
  7.                         Case "ASSEMBLY":
  8.                             If attPart.TextString = "" Then
  9.                                 Exit For
  10.                             Else:
  11.                                 Assembly = attPart.TextString
  12.                             End If
  13.                     End Select
  14.                 Next attPart
  15.                
  16.                 'SQLstring = "Select PartID, QTY, AssemblyType From tblAssembly Where AssemblyName ='" & Assembly & "';"
  17.                 SQLstring = "Select tblAssembly.PartID, tblAssembly.Quantity, tblAssembly.AssemblyType, tblParts.PartName From tblAssembly Inner Join tblParts ON tblAssembly.PartID = tblParts.PartID Where tblAssembly.AssemblyName ='" & Assembly & "';"
  18.                 Set PlumbingREC = PlumbingDB.OpenRecordset(SQLstring, dbOpenDynaset)
  19.                
  20.                 If PlumbingREC.RecordCount > 0 Then
  21.                     If PlumbingREC.EOF And PlumbingREC.BOF Then
  22.                         Exit Sub
  23.                     Else:
  24.                         PlumbingREC.MoveFirst
  25.                         Y = 0
  26.                         Do While Not PlumbingREC.EOF
  27.                         
  28.                             ReDim roughPart(Y, cntAssembly) As String
  29.                             ReDim roughPartQty(Y, cntAssembly) As Integer
  30.                            
  31.                             If PlumbingREC.Fields("AssemblyType").value = "Rough" Then
  32.                                 roughPart(Y, cntAssembly) = PlumbingREC.Fields("PartName").value
  33.                                 roughPartQty(Y, cntAssembly) = PlumbingREC.Fields("Quantity").value
  34.                                 Y = Y + 1
  35.                             End If
  36.                            
  37.                             PlumbingREC.MoveNext
  38.                         Loop
  39.                         
  40.                         PlumbingREC.MoveFirst
  41.                         Y = 0
  42.                         Do While Not PlumbingREC.EOF
  43.                         
  44.                             ReDim topoutPart(Y, cntAssembly) As String
  45.                             ReDim topoutPartQty(Y, cntAssembly) As Integer
  46.                            
  47.                             If PlumbingREC.Fields("AssemblyType").value = "TopOut" Then
  48.                                 topoutPart(Y, cntAssembly) = PlumbingREC.Fields("PartName").value
  49.                                 topoutPartQty(Y, cntAssembly) = PlumbingREC.Fields("Quantity").value
  50.                                 Y = Y + 1
  51.                             End If
  52.                            
  53.                             PlumbingREC.MoveNext
  54.                         Loop
  55.                     End If
  56.                 End If
  57.                
  58.                 PlumbingREC.Close
  59.                 Set PlumbingREC = Nothing

我正在尝试执行的操作的摘要:
我有1个块,“程序集”,其中1个属性“程序集”。 该属性的值是存储在 *.mdb 文件中的程序集名称。 因此,我的部件存储在数据库中,而不是存储在块中的多个属性中。
一个组件块可能具有仅引用一个或两个零件的装配体名称,但另一个模块可能具有 20 到 30 个零件。 因此,当我创建(redim)我的数组时...roughPart(X,Y) 第一列中可能只有 2 行数据,然后当查询下一个程序集块并且它有 30 个零件时,该列中将有 30 行。 这是一个问题吗??
提前感谢您的帮助...我希望这足够清楚,如果你需要,我会发布更多细节......
谢谢

本帖以下内容被隐藏保护;需要你回复后,才能看到!

游客,如果您要查看本帖隐藏内容请回复
回复

使用道具 举报

18

主题

55

帖子

1

银币

初露锋芒

Rank: 3Rank: 3Rank: 3

铜币
127
发表于 2006-11-16 13:27:22 | 显示全部楼层
出于某种原因,我认为您只能重新划分一维数组。
回复

使用道具 举报

18

主题

55

帖子

1

银币

初露锋芒

Rank: 3Rank: 3Rank: 3

铜币
127
发表于 2006-11-16 13:39:10 | 显示全部楼层

这意味着一旦您将数组的尺寸设置为其初始大小,如下所示:
ReDim粗糙部分(Y, cntAssembly)作为循环中的字符串
,每次后续迭代只能替换cntAssembly的新值。Y的值不能改变。
实现目标的一种方法是预先计算记录计数,以便在开始循环之前建立每个数组的第一个维度。
回复

使用道具 举报

6

主题

94

帖子

1

银币

初露锋芒

Rank: 3Rank: 3Rank: 3

铜币
118
发表于 2006-11-16 13:49:37 | 显示全部楼层
此时,我正在考虑调用一个过程,该过程将获取找到的数据并立即将其导出到Excel工作簿。 而不是查询所有数据并编译成数百个变量,然后将这些变量发送到电子表格。 我认为这将消除对我的阵列的需求...
你同意吗? 或者我错过了什么?
顺便说一句..
当我再次阅读ReDim的帮助时。它更有意义..
您只能更改最后一个维度的大小,而根本无法更改维度的数量。
因此,如果您从单个开始,则无法更改为2 dim数组。 但是,如果你有一个暗淡,你可以随时更改该暗淡的大小,如果你有一个多重暗淡的数组,你只能改变最后一个暗淡的大小(第2个表示双调,第3个表示3层,依此类推)。
这意味着,如果我想使用此方法,我必须将我的第一个 Dim(Rows)预设为足够大的 # 以容纳所有可能的零件,然后每当找到新组件时,我就可以更改第 2 个 dim(列)的 # 。
我认为立即将数据导出到excel比先将数据编译为数组要容易得多。 我会这样做,除非你们中的一个人有理由我不应该这样做。
再次感谢您的帮助...
回复

使用道具 举报

6

主题

94

帖子

1

银币

初露锋芒

Rank: 3Rank: 3Rank: 3

铜币
118
发表于 2006-11-16 13:54:40 | 显示全部楼层
谢谢你,查克
我预设它的问题有两个方面:
1。当我使用我的SQLstring查询我的数据库时,PlumbingREC的记录计数总是1。这是因为我在进行内部联接吗?
2.另一个问题是,查询的第一个块可能需要数组的第一个Dim为5个条目,而下一个块则可能需要30个条目。因此,在我看来,我必须对所有程序集进行记录计数,然后重新循环程序集,以将零件应用于数组
回复

使用道具 举报

6

主题

103

帖子

2

银币

初露锋芒

Rank: 3Rank: 3Rank: 3

铜币
127
发表于 2006-11-16 14:06:09 | 显示全部楼层
我完全同意立即导出到Excell,只要不需要重新处理数据(组合排序和过滤东西)
创建交错数组的解决方法是在这样的数组中使用集合类型:
  1. Sub test()
  2. Dim Cl() As New Collection
  3. ReDim Preserve Cl(2)
  4. Cl(0).Add ("YourStuff0.1")
  5. Cl(0).Add ("YourStuff0.2")
  6. Cl(0).Add ("YourStuff0.3")
  7. Cl(0).Add ("YourStuff0.4")
  8. Cl(1).Add ("YourStuff1.1")
  9. Cl(1).Add ("YourStuff1.2")
  10. Cl(1).Add ("YourStuff1.3")
  11. Cl(1).Add ("YourStuff1.4")
  12. Cl(2).Add ("YourStuff2.1")
  13. ReDim Preserve Cl(3)
  14. Cl(3).Add ("YourStuff3.1")
  15. Cl(3).Add ("YourStuff3.2")
  16. Cl(3).Add ("YourStuff3.3")
  17. Cl(3).Add ("YourStuff3.4")
  18. End Sub

你可以通过这种方式添加两个维度。缺点是,与真数组相比,收集对象的速度很慢。
因此,如果您不需要将数据保存在内存中...别这样。
回复

使用道具 举报

2

主题

25

帖子

2

银币

初来乍到

Rank: 1

铜币
33
发表于 2006-11-16 14:12:25 | 显示全部楼层

您是否移动到最后一条记录(. movelast),因为在所有记录真正加载到记录集中之前,计数不会显示实际大小
也许我弄错了您想要什么,但是如果您知道记录的数量和字段的数量(Recordset.Fields.Count),您可以重新设置一个数组。
另一方面,如果您已经在记录集中保存了数据,我认为没有必要在没有任何特殊原因的情况下将其传输到数组中。
回复

使用道具 举报

18

主题

55

帖子

1

银币

初露锋芒

Rank: 3Rank: 3Rank: 3

铜币
127
发表于 2006-11-16 15:08:42 | 显示全部楼层
Kevin,尝试使用GetRow方法:
这是我的程序代码片段:
  1. Private Sub UserForm_Initialize()
  2.     On Error GoTo ErrorHandler
  3.     Dim Cnxn As ADODB.Connection
  4.     Dim rstNew As ADODB.Recordset
  5.     Dim strCnxn As String
  6.     Dim strSQL As String
  7.     Dim retArr As Variant
  8.     Dim i As Long, j As Long
  9.    
  10.     ' Open connection
  11.    
  12.     strCnxn = "Provider=Microsoft.Jet.OLEDB.4.0;" & _
  13.                "Data Source=D:\AUTOLISP\LISPS\VBA\ACCESS\data.mdb;"
  14.     Set Cnxn = New ADODB.Connection
  15.     Cnxn.Open strCnxn
  16.    
  17.     ' Open table
  18.    
  19.     Set rstNew = New ADODB.Recordset
  20.     strSQL = "prot_Viabilita" ' table name
  21.     rstNew.Open strSQL, Cnxn, adOpenKeyset, adLockOptimistic, adCmdTable
  22.     [b]retArr = rstNew.GetRows[/b]
  23.     Me.Caption = "Data from table " & Chr(34) & strSQL & Chr(34)
  24.     '
  25.     ' Transpose record data and populate list box
  26.     '
  27.     ReDim dataArr(UBound(retArr, 2), UBound(retArr, 1)) As String
  28.     For i = 0 To UBound(retArr, 1)
  29.     For j = 0 To UBound(retArr, 2)
  30.     dataArr(j, i) = CStr(retArr(i, j))
  31.     Next
  32.     Next
  33.     ListBox1.List() = dataArr
  34.     ' clean up
  35.     rstNew.Close
  36.     Cnxn.Close
  37.     Set rstNew = Nothing
  38.     Set Cnxn = Nothing
  39.     Exit Sub
  40.    
  41. ErrorHandler:
  42.     ' clean up
  43.     If Not rstNew Is Nothing Then
  44.         If rstNew.State = adStateOpen Then rstNew.Close
  45.     End If
  46.     Set rstNew = Nothing
  47.    
  48.     If Not Cnxn Is Nothing Then
  49.         If Cnxn.State = adStateOpen Then Cnxn.Close
  50.     End If
  51.     Set Cnxn = Nothing
  52.    
  53.     If Err  0 Then
  54.         MsgBox Err.Source & "-->" & Err.Description, , "Error"
  55.     End If
  56. End Sub

>'J'<
回复

使用道具 举报

2

主题

25

帖子

2

银币

初来乍到

Rank: 1

铜币
33
发表于 2006-11-17 07:34:10 | 显示全部楼层
能不能把y和cntassembly变量调换一下,让y是最后一个?
回复

使用道具 举报

154

主题

1274

帖子

8

银币

顶梁支柱

Rank: 50Rank: 50

铜币
1936
发表于 2006-11-17 11:18:09 | 显示全部楼层
晚了一天,少了一分钱。   
回复

使用道具 举报

发表回复

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

  • 微信公众平台

  • 扫描访问手机版

  • 点击图片下载手机App

QQ|关于我们|小黑屋|乐筑天下 繁体中文

GMT+8, 2025-7-7 01:12 , Processed in 1.349418 second(s), 72 queries .

© 2020-2025 乐筑天下

联系客服 关注微信 帮助中心 下载APP 返回顶部 返回列表