• generic code

    Author
    Topic
    #354392

    How to create a generic code

    I must write 7 functions containing one and the same sql query depending on the choice
    made in the basic table(FOrderIndformation).
    The code that i must repeat 7 times, in each of the 7 function is the following:

    Dim afid As String
    Dim strDocName As String
    strDocName = “FOrderinformation”
    If IsOpen(strDocName) = True Then
    Dim office As Control
    Set office = Forms![FOrderInformation]![office]
    Dim SQLLondon As String
    dim sqlBerlin as string
    SQLLondon = “AND ((Customers.afid)=1)”
    SQLBerlin = “AND ((Customers.afid)=2)”
    If office = 1 Then
    afid = SQLLondon
    ElseIf office = 2 Then
    afid = SQLBerlin
    end if

    The above coding gives me the string afid, which i concatenate with the basic sql
    called by me SQLBas, for example:
    Dim SQLCustomerOrder As String
    Dim SQLBas As String
    SQLBas = ( a valid sql query)
    SQLCustomerOrder = SQLBas & afid
    Forms!frmCustomerOrders.RecordSource = SQLCustomerOrder

    I have 6 more similar functions.My question is how to spare time and space
    and eliminate writing one and the same code to define the the sql which i call afid?

    Viewing 0 reply threads
    Author
    Replies
    • #520639

      Put that code in a function, like this:

      Public Function Getafid() as String
      Dim strDocName As String
          strDocName = "FOrderinformation"
          If IsOpen(strDocName) = True Then
              Dim office As Control
              Set office = Forms![FOrderInformation]![office]
              If office = 1 Then
                  Getafid = "AND ((Customers.afid)=1)"
              ElseIf office = 2 Then
                  Getafid = "AND ((Customers.afid)=2)"
              End If
          End If
      End Function
      
      Then you can do this:
      
      
      Dim SQLCustomerOrder As String
      Dim SQLBas As String
          SQLBas = ( a valid sql query)
          SQLCustomerOrder = SQLBas & Getafid()
      
    Viewing 0 reply threads
    Reply To: generic code

    You can use BBCodes to format your content.
    Your account can't use all available BBCodes, they will be stripped before saving.

    Your information: