【问题标题】:Text boxes not retaining values文本框不保留值
【发布时间】:2023-03-06 12:05:01
【问题描述】:

论坛新手。

我正在设计的数据库存在问题,该数据库将在打印机上进行预防性维护时进行记录。一切都有效,直到我尝试将值转移到另一种形式。我想要做的是从一个表单上的文本框中获取 2 个值并将它们移动到另一个表单。我可以将这些值保存到全局变量中(这是我可以让它工作的唯一方法),我可以从第二种形式的全局变量中获取值,它们会像我想要的那样显示在文本框中。我的问题是当我关闭表单或关闭数据库时,全局变量会丢失它们的值,所以我尝试让文本框使用 .defaultvalue 和 .value 属性保存值,但它不起作用。这是我在文本框中显示全局变量的代码:

    Private Sub Form_load()

'Variables for this form

'301

Dim date301Jan As Date
Dim date301Apr As Date
Dim date301Jul As Date
Dim date301Oct As Date
Dim data301TechJan As String


'Converting global variables to the text boxes in this form
date301Jan = var301Jan
date301Apr = var301Apr
date301Jul = var301Jul
date301Oct = var301Oct
data301TechJan = var301TechJan

Me.txt301Jan = date301Jan
Me.txt301Apr = date301Apr
Me.txt301Jul = date301Jul
Me.txt301Oct = date301Oct
Me.txt301TechJan = data301TechJan



End Sub

这可行,但不会保存文本框中的值。我尝试了其他方法来保存值,但它们不起作用。我尝试了一个隐藏的表格,甚至一个表格来获取值,但是表格有限制,我的表格不能从多个表格中提取......我计算出我需要超过 255 个限制。我也会附上一张我的“结束游戏”表格的照片。我想设置这个表单,一旦它填满,它会提示用户保存它,然后清除所有内容以重新开始。没关系,我还没有发布图片的授权。

无论如何,我会尽力描述这张照片。我有一个表格,上面有多台机器,每台机器有 8 个文本框和 4 个标签。标签是总理到期的月份。 4 个文本框用于 PM 完成的季度(日期),另外 4 个用于负责完成 PM 的技术。总共有64台机器,所以表格很大。

我搜索了多个论坛,我看到 1 说将“end sub”更改为“exit sub”,看看是否可行,但没有。调试器说类似“预期的结束子”。我没有对其余的机器进行编码,因为我无法保存它。

任何关于如何解决我的问题的意见将不胜感激。

【问题讨论】:

  • 好的,所以问题 1 - 我们为什么要尝试将值存储在表单上的控件中?您知道您正在使用数据库,对吗?这意味着这些值可以存储在表格中,然后在打开的表格中调用?我的 POV 似乎做了很多额外的工作。另一件事-您不需要全局值来完成此操作-您可以从其他表单引用表单控件,甚至可以以这种方式移动值。我认为因为您是新手,所以我们可能没有完全掌握您可用的选项
  • 我已经 20 年没有用 VBA 做过任何事情了,所以我在这方面几乎是个新手。我尝试通过表单控件传递值,但它并没有做我想做的事情。该表的限制为 255 列,我需要大约 500 列,因此使用表已被淘汰。我正在考虑将文本框更改为标签并仅更改标题,以便即使数据库关闭并重新打开,文本也存在。

标签: vba ms-access


【解决方案1】:

发现标签会起作用。我使用我的“form1”并在设计视图中打开“form2”并隐藏并放置 vba 代码来编辑标签的标题,然后保存“form2”。我关闭并打开“form2”,标题按我想要的方式保存。我可以摆脱我的全局变量,因为我不再需要它们。下面是“form1”的样子:

Private Sub Push_Click()
'Variable for date if PM was completed outside of required completion month

Dim newdate As Date
Dim newTech As String


'When Push is clicked, adds all values to the Log Table

    CurrentDb.Execute "INSERT INTO Log([GE Printer], [Date Completed], [Report Type], [Pre 12mW], [Pre 25mW], [Pre 38mW], [Pre 50mW], [Pre 75mW], [Pre 100mW], [Pre 150mW], [Pre 200mW], [Post 12mW], [Post 25mW], [Post 38mW], [Post 50mW], [Post 75mW], [Post 100mW], [Post 150mW], [Post 200mW], [X Notch Freq], [X Gain], [X FF], [X VFF], [Y Notch Freq], [Y Gain], [Y FF], [Y VFF], [Encoder Cal], [Laser SN], [Laser Head SN], [Install HRS], [Current HRS], [Warranty HRS], [Blade Gap], [Blade Gap Offset], Threshold, [Laser RFID], [Cart RFID], [VAT RFID], [Old X Beamsize], [New X Beamsize], [Old Y Beamsize], [New Y Beamsize], ADC, Notes, [Laser Focus], Technician, [Total Laser HRS], [Printer SN]) " & _
               " VALUES(" & Me.GE_ & ",'" & Me.txtDate & "','" & Me.txtReport & "','" & Me.Pre12 & "','" & Me.Pre25 & "','" & Me.Pre38 & "','" & Me.Pre50 & "','" & Me.Pre75 & "','" & Me.Pre100 & "','" & _
               Me.Pre150 & "','" & Me.Pre200 & "','" & Me.Post12 & "','" & Me.Post25 & "','" & Me.Post38 & "','" & Me.Post50 & "','" & Me.Post75 & "','" & Me.Post100 & "','" & Me.Post150 & "','" & _
               Me.Post200 & "','" & Me.XNotch & "','" & Me.XGain & "','" & Me.XFF & "','" & Me.XVFF & "','" & Me.YNotch & "','" & Me.YGain & "','" & Me.YFF & "','" & Me.YVFF & "','" & Me.Encoder & "','" & _
               Me.SN & "','" & Me.HeadSN & "','" & Me.Install & "','" & Me.Current & "','" & Me.Warranty & "','" & Me.PhyGap & "','" & Me.Offset & "','" & Me.Threshold & "','" & Me.Laser & "','" & _
               Me.Cart & "','" & Me.Vat & "','" & Me.BeforeX & "','" & Me.AfterX & "','" & Me.BeforeY & "','" & Me.AfterY & "','" & Me.ADC & "','" & Me.Notes & "','" & Me.Focus & "','" & Me.Tech & "','" & _
               Me.Total & "','" & Me.Asset_ & "')"

newTech = CStr(Me.Tech)
DoCmd.OpenForm "PMsch", acDesign, , , , acHidden

'Applies info to the PMSch form

    '301

    If Me.GE_ = "301" Then
        If DatePart("m", Me.txtDate) = 1 Then
            Forms("PMsch")("lbl301Jan").Caption = Me.txtDate
            Forms("PMsch")("lbl301TechJan").Caption = newTech
        ElseIf DatePart("m", Me.txtDate) = 4 Then
            Forms("PMsch")("lbl301Apr").Caption = Me.txtDate
            Forms("PMsch")("lbl301TechApr").Caption = newTech
        ElseIf DatePart("m", Me.txtDate) = 7 Then
            Forms("PMsch")("lbl301Jul").Caption = Me.txtDate
            Forms("PMsch")("lbl301TechJul").Caption = newTech
        ElseIf DatePart("m", Me.txtDate) = 10 Then
            Forms("PMsch")("lbl301Oct").Caption = Me.txtDate
            Forms("PMsch")("lbl301TechOct").Caption = newTech
        Else
            newdate = InputBox("Please input Date as m/d/yyyy.")
            If DatePart("m", newdate) = 12 Or 2 Then
                Forms("PMsch")("lbl301Jan").Caption = newdate
                Forms("PMsch")("lbl301TechJan").Caption = newTech
            ElseIf DatePart("m", newdate) = 3 Or 5 Then
                Forms("PMsch")("lbl301Apr").Caption = newdate
                Forms("PMsch")("lbl301TechApr").Caption = newTech
            ElseIf DatePart("m", newdate) = 6 Or 8 Then
                Forms("PMsch")("lbl301Jul").Caption = newdate
                Forms("PMsch")("lbl301TechJul").Caption = newTech
            ElseIf DatePart("m", newdate) = 9 Or 11 Then
                Forms("PMsch")("lbl301Oct").Caption = newdate
                Forms("PMsch")("lbl301TechOct").Caption = newTech
            End If
        End If
    End If

现在我只需要完成其他所有代码的编码并准备好与其他技术人员一起测试它。关闭/保存的 docmd 位于最后一台打印机的最底部,因此它没有显示在我的代码中。

【讨论】:

  • 我的意思是我很高兴你想通了,但你所做的只是一个 hack。学习我上面建议的东西(谷歌实际上会很快让你到达那里)。最好在有沙盒时间的时候学习。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-11-01
  • 2013-01-08
  • 2015-09-27
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多