Attribute VB_Name = "modUtil"
Option Compare Database
Option Explicit

Public gCurrentBatchID As Long

Public Function GetNextBatchNumber() As Long
    Dim db As DAO.Database
    Dim rs As DAO.Recordset
    Set db = CurrentDb()
    Set rs = db.OpenRecordset("SELECT Max(BatchID) AS MaxID FROM tblImportBatch")
    If IsNull(rs!MaxID) Then
        GetNextBatchNumber = 1
    Else
        GetNextBatchNumber = rs!MaxID + 1
    End If
    rs.Close
    Set rs = Nothing
    Set db = Nothing
End Function

Public Sub LogError(batchID As Long, rowNo As Long, msg As String)
    Dim db As DAO.Database
    Set db = CurrentDb()
    db.Execute "INSERT INTO tblErrorLog (BatchID, RowNo, ErrorMsg, [Timestamp]) " & _
               "VALUES (" & batchID & ", " & rowNo & ", '" & Replace(msg, "'", "''") & "', Now())"
    Set db = Nothing
End Sub

Public Function FormatDateForSql(d As Date) As String
    FormatDateForSql = "#" & Format(d, "mm/dd/yyyy") & "#"
End Function

Public Function FormatNumberForSql(n As Currency) As String
    ' Str() always uses "." as the decimal separator regardless of Windows
    ' locale (unlike & concatenation or CStr, which use the locale's decimal
    ' separator - e.g. "," on Vietnamese Windows). Concatenating a Currency
    ' value directly into SQL text on such a locale silently inserts an
    ' extra comma, breaking the statement's value count. Str() also prefixes
    ' a space for non-negative numbers, hence the Trim.
    FormatNumberForSql = Trim(Str(n))
End Function
