【问题标题】:MS Access textbox update/appendMS Access 文本框更新/追加
【发布时间】:2012-07-07 06:27:46
【问题描述】:

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

【问题讨论】:

    标签: ms-access vba


    【解决方案1】:
    Forms!Form3!Text5 = strResult
    

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

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

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

    【讨论】:

      【解决方案2】:

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

      【讨论】:

      • strResult 没有在循环内声明(我猜它在模块/类级别)。即使是 vba 范围规则也......不同。一旦声明了一个变量,即使它是一个循环,它也不会重新初始化。另一个奇怪的事实是,如果它是在循环中声明的,那么如果在相同的方法中,它可以在循环之后使用。这可能就是为什么 vba 程序员倾向于在顶部声明所有内容的原因。
      • 我猜它根本没有声明,正如 Option Compare Database 所证明的那样,它通常表示文件的顶部,更重要的是缺少 Option ExplicitDim P As Integer 'this value is the prod number 的声明后来使用prodnumber 没有声明。无论如何,我建议的更改应该使它工作,如果不是最理想的
      猜你喜欢
      • 1970-01-01
      • 2021-12-26
      • 2022-07-04
      • 1970-01-01
      • 2023-03-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多