Assigning a With statement to one of two classes depending on a toggle, issues assigning New Object to be the same as another

huangapple go评论54阅读模式
英文:

Assigning a With statement to one of two classes depending on a toggle, issues assigning New Object to be the same as another

问题

我目前有点困扰于"对象变量或With块变量未设置错误"。

对于使用With语句来简化我的代码,我还是相对新手。我的类"ContractSelection"有两个实例,分别是previousContract和currentContract。这两个实例都已被声明为公共变量,并已赋予值。在这个子程序中,我试图根据用户界面中的切换按钮,提交一部分信息,具体取决于他们是查看当前选择还是以前的选择。

老实说,我不确定contractToUpdate = currentContract这个语句是否有效,但我发现很难通过简单的谷歌搜索来解决这个问题。

在一个公共变量模块中,声明了如下的公共变量:

  Public currentContract As ContractSelection
  Public previousContract As ContractSelection

在用户界面模块中,初始化了currentContract和previousContract:

Private Sub UserForm_Initialize()
  Set currentContract = New ContractSelection
  Set previousContract = New ContractSelection
End Sub

值是在一般子程序中设置的,例如:

Sub setThePreviousContractAsTheCurrent()
   currentContract.DistrictNumber = previousContract.DistrictNumber
   currentContract.ContractName = previousContract.ContractName
   currentContract.RegionName = previousContract.RegionName
   '...
End Sub

在主子程序模块中,出现了问题的子程序如下:

Sub submitNewCode()
  Dim contractToUpdate As ContractSelection, response As Integer
  Set contractToUpdate = New ContractSelection
  If CDBENC_Form.chkbx_PreviousSearch.value = False Then
     '问题似乎出现在这里
     contractToUpdate = currentContract
  Else
     contractToUpdate = previousContract
  End If
  With contractToUpdate
   If .CodeOfContract <> "" Then
     If isSimilarByOne(.CodeOfContract, CDBENC_Form.txt_Code.value) = False Then
       dataSheet.Cells(.TheRowIWasFoundIn, dataMappedColumns.CodeColumnNum).value = CDBENC_Form.txt_Code.value
     Else
        response = MsgBox("新代码与原代码相似,确定要使用 " & CDBENC_Form.txt_Code.value & " 作为新代码吗?", vbYesNo + vbQuestion, "确认操作")
         If response = vbYes Then
            dataSheet.Cells(.TheRowIWasFoundIn, dataMappedColumns.CodeColumnNum).value = CDBENC_Form.txt_Code.value
         Else
            Exit Sub
         End If
     End If
   End If
  End With
End Sub

我尝试检查currentContract是否出现为"Nothing"的情况,但结果都会进入"Else"分支:

If currentContract Is Nothing Then
 MsgBox "当前合同为空"
 Exit Function
Else
 MsgBox "当前合同不为空"
End If

我还尝试了以下两种方式来声明contractToUpdate:

Dim contractToUpdate As ContractSelection
Dim contractToUpdate As New ContractSelection

还有将公共变量也放入声明:

Public contractToUpdate As New ContractSelection

任何建议都会有所帮助,我觉得我离解决方案很近,但离解决还有一段距离。

英文:

I'm currently a bit stuck with the "Object Variable or With block variable not set error".

Still fairly new to using With statements to simplify my code, I have two instances on my Class "ContractSelection" Both existing instances (previousContract & currentContract) are both by this time in the code called as public variables, and set with values. In this Sub I am attempting to submit one piece of information depending whether they are looking at the current selection or the previous (a toggle in the userform).
Frankly I'm not sure if contractToUpdate = currentContract is even a valid statement, but i'm finding it difficult to simply google.

(in a Public Variable module)

  Public currentContract As ContractSelection
  Public previousContract As ContractSelection

(in Userform module)

Private Sub UserForm_Initialize()
  Set currentContract = New ContractSelection
  Set previousContract = New ContractSelection
End Sub

Values are set in general Subs like this

Sub setThePreviousContractAsTheCurrent()
   currentContract.DistrictNumber = previousContract.DistrictNumber
   currentContract.ContractName = previousContract.ContractName
   currentContract.RegionName = previousContract.RegionName
&#39;...
End Sub

(In a Main Sub module) This Sub is where the issue is.

Sub submitNewCode()
  Dim contractToUpdate As ContractSelection, response As Integer
  Set contractToUpdate = New ContractSelection
  If CDBENC_Form.chkbx_PreviousSearch.value = False Then
     &#39;vba stating the issue is here
     contractToUpdate = currentContract
  Else
     contractToUpdate = previousContract
  End If
  With contractToUpdate
   If .CodeOfContract &lt;&gt; &quot;&quot; Then
     If isSimilarByOne(.CodeOfContract, CDBENC_Form.txt_Code.value) = False Then
       dataSheet.Cells(.TheRowIWasFoundIn, dataMappedColumns.CodeColumnNum).value = CDBENC_Form.txt_Code.value
     Else
        response = MsgBox(&quot;The new code is close to the original, is &quot; &amp; CDBENC_Form.txt_Code.value &amp; &quot; the intended new code?&quot;, vbYesNo + vbQuestion, &quot;Confirm Action&quot;)
         If response = vbYes Then
            dataSheet.Cells(.TheRowIWasFoundIn, dataMappedColumns.CodeColumnNum).value = CDBENC_Form.txt_Code.value
         Else
            Exit Sub
         End If
     End If
   End If
  End With
End Sub

I've tried checking for to see if for some reason currentContract is showing as nothing

this returns the else

If currentContract Is Nothing Then
 MsgBox &quot;Current Contract is nothing&quot;
 Exit Function
Else
 MsgBox &quot;Current Contract is not nothing&quot;
End If

I've tried both

Dim contractToUpdate As ContractSelection
Dim contractToUpdate As New ContractSelection

also putting in the public variables as well

Public contractToUpdate As New ContractSelection

Any suggestions help, I feel as though I'm close to the idea but far from the solution.

答案1

得分: 0

VBA在将值分配给对象类型的变量时需要使用Set。所以:

如果 CDBENC_Form.chkbx_PreviousSearch.value = False Then
    Set contractToUpdate = currentContract
Else
    Set contractToUpdate = previousContract
End If

如果你对为什么这样做感兴趣:https://stackoverflow.com/a/9924325/478884

英文:

VBA requires the use of Set when assigning a value to an object-typed variable.
So:

If CDBENC_Form.chkbx_PreviousSearch.value = False Then
    Set contractToUpdate = currentContract
Else
    Set contractToUpdate = previousContract
End If

If you're interested in why that's the case: https://stackoverflow.com/a/9924325/478884

huangapple
  • 本文由 发表于 2023年2月9日 02:44:13
  • 转载请务必保留本文链接:https://go.coder-hub.com/75390382.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定