1

我有一些代码填充了一个名为text5 Forms!Form3!text5. 但是每次我单击更新此文本框的按钮时,它都会刷新它。

我希望文本保持固定,并在每次单击按钮时添加新数据。我已经附上了我到目前为止所做的代码

Option Compare Database

Private Sub Command0_Click()

  Set db = CurrentDb

  Dim I As Integer
  Dim varNumber As Integer  ' this takes the number for how many times to loop
  Dim strQueryName As String  ' this is for the sql to find lowest rack number
  Dim P  As Integer  'this value is the prod number
  Dim x As Integer   'value from lowestrackSQL



  varNumber = Me.Quantity 'box from form me means this form
  prodnumber = Me.ProdNo  'box from form

  strQueryName = "SQLToFindLowestRackNumber"   'this will be used to execute the query


  strSQL = CurrentDb.QueryDefs(strQueryName).sql   ' this stores the sql but does not run it

  Forms!form3!txtPrint = strResult
  'Stop

  For I = 1 To varNumber  ' uses the quntity value to count how many times to loop

    x = DLookup("locationrack", strQueryName)  'puts value of query into value x
    prod# = prodnumber

    'below puts into imediate view box
    Debug.Print "Line number = " & I; ";  Rack Location = " & x; ";   Product Number = " & prod#; ";"
    'below puts it into form3 text box
    strResult = strResult & " Line Number = " & I & " Rack Location = " & x & "   Product Numner = " & prod# & vbCrLf & ""
    Forms!form3!Text5 = strResult


    'below executes the SQL
    DoCmd.SetWarnings False
    DoCmd.RunSQL "UPDATE [Location] SET [Location].ID = 0 WHERE [Location].RackID =" & x
    DoCmd.SetWarnings True

 Next I



End Sub   

正如您所看到的,值strResult被传递到文本框,我只想继续将值添加到文本框,即使在我再次重新启动循环后也是如此。

4

2 回答 2

1

尝试使用其他变量在循环之外声明 strResult:

Dim I As Integer
Dim varNumber As Integer  ' this takes the number for how many times to loop
Dim strQueryName As String  ' this is for the sql to find lowest rack number
Dim P  As Integer  'this value is the prod number
Dim x As Integer   'value from lowestrackSQL
Dim strResult as String

因为我认为它每次都会清除它,因为它的范围仅限于 for 循环内

另外,你说的地方

Forms!form3!txtPrint = strResult

在下面添加

strResult = Forms!form3!text5
于 2012-07-06T17:25:19.293 回答
1
Forms!Form3!Text5 = strResult

...用变量的值覆盖文本框的内容。

附加值而不是覆盖它,您需要执行以下操作:

Forms!Form3!Text5 = Forms!Form3!Text5 & strResult
于 2012-07-06T22:25:52.607 回答