A family of Microsoft relational database management systems designed for ease of use.
Here is a new version that exports text files. Before testing it, change the export path in the variable strPath. It MUST end in a backslash.
Private Sub cmdAssignTest_Click()
Dim strCongregationID As String
Dim strUserID As String
Dim lngBatchSize As Long
Dim lngOldTerritoryID As Long
Dim lngNewTerritoryID As Long
Dim dbs As DAO.Database
Dim rst As DAO.Recordset
Dim rst2 As Recordset
Dim strSQL As String
Dim lngRecordCount As Long
Dim lngCurrentRecord As Long
Dim strTerritoryName As String
Dim lngTerritoryNumber As Long
Dim f As Integer
Dim strPath As String
Dim strLine As String
Dim strDelimiter As String
On Error GoTo ErrHandler
' Change these as needed
' Batch size
lngBatchSize = 30
' Unassigned ID
lngOldTerritoryID = 106
' Path for text files
strPath = "C:\Surveys"
' Delimiter for text files
strDelimiter = vbTab ' can also be ","
' Get some info
strCongregationID = DLookup("CongregationID", "tblVar")
strUserID = DLookup("UserID", "tblVar")
strTerritoryName = Nz(Me.TerritoryName, InputBox("Please provide a name for the new territory"))
' Get number of unassigned survey records
strSQL = "SELECT * FROM tblSurveyData WHERE CityID=" & strCityID & _
" AND TerritoryTypeID=" & strTerritoryTypeID & _
" AND TerritoryID=" & lngOldTerritoryID
Set dbs = CurrentDb
Set rst = dbs.OpenRecordset(strSQL, dbOpenDynaset)
If rst.EOF Then
MsgBox "No records!", vbExclamation
GoTo ExitHandler
End If
rst.MoveLast
rst.MoveFirst
lngRecordCount = rst.RecordCount
f = FreeFile
' Get highest TerritoryNumber
lngTerritoryNumber = Nz(DMax("TerritoryNumber", "tblTerritory", _
"CityID=" & strCityID & " AND TerritoryTypeID=" & strTerritoryTypeID), 0)
Set rst2 = dbs.OpenRecordset("tblTerritory", dbOpenDynaset)
' Loop
lngCurrentRecord = 0
Do While Not rst.EOF
lngCurrentRecord = lngCurrentRecord + 1
If lngCurrentRecord Mod lngBatchSize = 1 Then
' Do we have enough records left?
If lngRecordCount - lngCurrentRecord >= lngBatchSize \ 2 _
Or lngCurrentRecord = 1 Then
' Create new territory
lngTerritoryNumber = lngTerritoryNumber + 1
rst2.AddNew
rst2!TerritoryNumber = lngTerritoryNumber
rst2!TerritoryTypeID = strTerritoryTypeID
rst2!CityID = strCityID
rst2!CongregationID = strCongregationID
rst2!EnteredBy = strUserID
rst2!TerritoryName = strTerritoryName
' Remember new ID
lngNewTerritoryID = rst2!TerritoryID
rst2.Update
' Close previous text file
If lngCurrentRecord > 1 Then
Close #f
End If
' Open new text file
Open strPath & "Territory" & lngNewTerritoryID & ".txt" For Output As #f
End If
End If
' Assign territory
rst.Edit
rst!TerritoryID = lngNewTerritoryID
rst.Update
' Build line for text file
strLine = rst!SurveyID & strDelimiter & Chr(34) & rst!FullName & Chr(34) & _
strDelimiter & Chr(34) & rst!StNumber & " "
If rst!Address2 = "1/2" Then
strLine = strLine & rst!Address2 & " "
End If
strLine = strLine & rst!StName & Chr(34) & strDelimiter & Chr(34)
Select Case True
Case rst![Apt?]
strLine = strLine & "Apt. "
Case rst![Room?]
strLine = strLine & "Room "
Case rst![Studio?]
strLine = strLine & "Studio "
Case rst![Suite?]
strLine = strLine & "Suite "
Case rst![Unit?]
strLine = strLine & "Unit "
Case rst![Space?]
strLine = strLine & "Space "
Case rst![Lot?]
strLine = strLine & "Lot "
Case rst![Bldg?]
strLine = strLine & "Bldg "
Case rst![Floor?]
strLine = strLine & "Floor "
End Select
strLine = strLine & rst!Address2 & Chr(34) & strDelimiter & _
Chr(34) & DLookup("City", "tblCity", "CityID=" & strCityID) & Chr(34) & _
strDelimiter & rst!Zip & strDelimiter & lngNewTerritoryID
' Write line to file
Print #f, strLine
' On to the next record
rst.MoveNext
Loop
ExitHandler:
On Error Resume Next
Close #f
rst.Close
rst2.Close
Set dbs = Nothing
Me.Requery
Exit Sub
ErrHandler:
MsgBox Err.Description, vbExclamation
Resume ExitHandler
End Sub