我有一些代碼填充名爲text5 Forms!Form3!text5
的文本框。但每次我點擊更新這個文本框的按鈕刷新它。MS Access文本框更新/追加
我喜歡它,所以文本保持不變,每次單擊按鈕時添加新數據。我重視的代碼,我至今
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
值傳遞到文本框,我只是想繼續增加值,以我重新啓動甚至在文本框中再次循環。
strResult不循環(我猜它在模塊/類級別)的內部聲明。即使它是vba範圍規則是...不同。一旦變量被聲明,即使它是一個循環,它也不會被重新初始化。另一個奇怪的事實是,如果它是在循環中聲明的,它可以在循環後用於相同的方法。這可能就是爲什麼vba程序員傾向於在頂部聲明一切。 – 2012-07-06 22:00:01
我猜它沒有被聲明,正如「Option Compare Database」所證明的那樣,它通常表示文件的頂部,更重要的是缺少「Option Explicit」和「Dim P As Integer」的聲明價值是產品編號 '隨着'prodnumber'的後續使用而沒有聲明。無論如何,我建議的變化應該使其工作,如果不理想 – Ghost 2012-07-06 22:05:48
啊我錯過了。 – 2012-07-06 22:25:26