MS Access文本框更新/附加

时间:2012-07-06 17:12:45

标签: ms-access vba

我有一些代码填充了一个名为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的值被传递到文本框,我只想继续将值添加到文本框中,即使我再次重新启动循环。

2 个答案:

答案 0 :(得分: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

答案 1 :(得分:1)

Forms!Form3!Text5 = strResult

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

追加该值而不是覆盖它,您需要这样做:

Forms!Form3!Text5 = Forms!Form3!Text5 & strResult