从具有唯一编号的另一个字段值创建串联代码 In Access VBA

Creating a Concatenated Code from another field value with unique number In Access VBA

我正在生成一个供应商表格,我将在其中添加 Supplier_NameSupplier_Code 是通过获取 Supplier_Name 的前两个字符并添加一个唯一编号自动生成的当 2 个供应商的前 2 个字符相同时,例如:

我是 VBA 的新手并尝试了以下方法但它不起作用:

Private Sub Supplier_Name_AfterUpdate()
    Dim DB As Database
    Dim RS As Recordset
    Dim SQL As String
    Dim var1 As String

    var1 = Left(Me.Supplier_Name.Value, 2)

    SQL = "SELECT Supplier_ID, LEFT(Supplier_Name,2) AS charsupplier, count (Supplier_Name) AS countSupplier " _
        & "FROM Suppliers " _
        & "WHERE charsupplier = var1 " _
        & "ORDER BY Supplier_ID"

     Set DB = CurrentDb
     Set RS = DB.OpenRecordset(SQL, dbOpenDynaset)

    Me.Supplier_Code = var1 & Format$(RS!countSupplier, "00")

    End Sub

如果有人可以提供帮助或建议替代方法,那将非常感谢。

EDIT: I think the flaw in my approach may be that if this is on the new record, the supplier_ID is not saved to the table and will not be available for query?

这里有一些东西可以让你基于名为 cmdAddNewSupplier 的按钮开始。您应该有足够的知识来使示例适应您的需要。 (抱歉,我进行了编辑,因为我忘了提及示例代码使用的是名为 [SUPPLIERS] 的虚构 table,其虚构列名称为 [SUPPLIER_NAME];您需要替换为您的 table 和列的名称。)

Private Sub cmdAddNewSupplier_Click()
  'TODO: create some error handling
  'TODO: check for null value of SupplierName Textbox; notify user and exit if its null
  'TODO: check for less than 2 chars for supplier name; notify user and exit if it's less than 2 chars (seems impossible, but can happen)
  Dim strSupCode As String 'the eventual unique id of the new supplier

  'make sure the user wants to add the supplier if the name already exists
  'all we are doing is utilizing DLookup so we don't have to deal with recordset object
  If Not IsNull(DLookup("[SUPPLIER_NAME]", "[SUPPLIERS]", "[SUPPLIER_NAME] = " & Chr(34) & Me.Supplier_Name & Chr(34))) Then
    If MsgBox(Me.Supplier_Name & " Already Exists!" & vbCrLf & "Are you sure that you want to add them?", vbYesNoCancel Or vbQuestion, "Please Confirm") <> vbYes Then
      Exit Sub
    End If
  End If

  strSupCode = GetSupplierCode(Me.Supplier_Name)

  MsgBox Me.Supplier_Name & vbCrLf & strSupCode 'test it out to make sure it's working before doing anything for real
End Sub

Function GetSupplierCode(strSupplierName As String) As String
  Dim nLoop As Long
  Dim strCode As String

  strCode = UCase(Left(strSupplierName, 2))

  'The supplier name is unique or the user means to add another supplier with the same name
  For nLoop = 1 To 100 '100 same names is unlikely, eh?

    'create a temp supplier code starting with 1 and increment
    'the return value is being set, so all we have to do is jump out of function when unique is found
    GetSupplierCode = strCode & Format(nLoop, "00")

    'TODO: utilize DLookup to check for existence; will leave this part to you;
    'dont forget you are looking at CODE and not NAME here as we did in the button click function

    '      if isnull(DLookup(<enter the required parameters>)) then
    '         exit function 'jump out of the function because this one should be the next unique
    '      end if

  Next

End Function

我已经使用以下代码解决了我的问题:

 Private Sub Supplier_Name_LostFocus()

If IsNull([Supplier_Code]) Then
   Dim DB As Database
   Dim RS As Recordset
   Dim var2 As String
   Dim var1 As String
   Dim var3 As String


   var1 = Left(Me.Supplier_Name, 2)

   var2 = "SELECT count(*) AS CountSupplier " & _
          "FROM Suppliers " & _
          "WHERE left(Suppliers.[Supplier_Name],2)='" & var1 & "';"
'   MsgBox (var2)



   Set DB = CurrentDb
   Set RS = DB.OpenRecordset(var2, dbOpenDynaset)
   var3 = RS!CountSupplier + 1
   Me.Supplier_Code = UCase(var1) & Format(var3, "00")
End If
End Sub