vba MS Access 文本框更新/追加

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11366767/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-11 16:47:01  来源:igfitidea点击:

MS Access textbox update/append

ms-accessvba

提问by Rob Hughes

I have some code which populates a text box called text5 Forms!Form3!text5. But everytime I click on the button which updates this text box it refreshes it.

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

I would like it so the text stay fixed, and add the new data everytime the button is clicked. I have attached the code, that I have done so far

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

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   

As you can see the value of strResultis passed to the text box and I just want to keep adding the value to the text box even after I restart the loop again.

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

回答by Ghost

Try declaring strResult outside of the loop with your other variables:

尝试使用其他变量在循环外声明 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

As it is I think it clears it every time because its scope is limited to within the for loop

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

Also, where you say

还有你说的地方

Forms!form3!txtPrint = strResult

Add underneath

在下面添加

strResult = Forms!form3!text5

回答by Christian Specht

Forms!Form3!Text5 = strResult

...overwritesthe content of the text box with the value of the variable.

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

To appendthe value instead of overwriting it, you need to do this:

附加价值,而不是覆盖它,你需要这样做:

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