1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:
61:
62:
63:
64:
65:
66:
67:
68:
69:
70:
71:
72:
73:
74:
75:
76:
77:
78:
79:
80:
81:
82:
83:
84:
85:
86:
87:
88:
89:
90:
91:
92:
93:
94:
95:
96:
97:
98:
99:
100:
101:
102:
103:
104:
105:
106:
107:
108:
109:
110:
111:
112:
113:
114:
115:
116:
117:
118:
119:
120:
121:
122:
123:
124:
125:
126:
127:
128:
129:
130:
131:
132:
133:
134:
135:
136:
137:
138:
139:
140:
141:
142:
143:
144:
145:
146:
147:
148:
149:
150:
151:
152:
153:
154:
155:
156:
157:
158:
159:
160:
161:
162:
163:
164:
165:
166:
167:
168:
169:
170:
171:
172:
173:
174:
175:
176:
177:
178:
179:
180:
181:
182:
183:
184:
185:
186:
187:
188:
189:
190:
191:
192:
193:
194:
195:
196:
197:
198:
199:
200:
201:
202:
203:
204:
205:
206:
207:
208:
209:
210:
211:
212:
213:
214:
215:
216:
217:
218:
219:
220:
221:
222:
223:
224:
225:
226:
227:
228:
229:
230:
231:
232:
233:
234:
235:
236:
237:
238:
239:
240:
241:
242:
243:
244:
245:
246:
247:
248:
249:
250:
251:
252:
253:
254:
255:
256:
257:
258:
259:
260:
261:
262:
263:
264:
265:
266:
267:
268:
269:
270:
271:
272:
273:
274:
275:
276:
277:
278:
279:
280:
281:
282:
|
Option Compare Database
Option Explicit
Function ImportSAAChanges()
Dim Ary() As String
Dim stTmp As String
Dim rs As dao.Recordset
Dim db As Database
Dim PathName As String
Dim StatusFilePathName As String
Dim StatusFilePathNametest As String
Dim TableName As String
Dim stTemp As String
Dim fileWpath As String
Dim IntRecordsProcessed As Integer
'Location of export file from the Sergeant At Arms Database
PathName = "Z:\SAA_Export1.txt"
'PathName = "C:\MIKE\ACCESS\OUTPUT\SAA_Export.txt"
'StatusFilePathName = "C:\MIKE\ACCESS\OUTPUT\HISTORY\SAA_Export-" & Date$ & ".txt"
StatusFilePathName = "Z:\HISTORY\SAA_Export-" & Date$ & ".txt"
StatusFilePathNametest = "Z:\HISTORY\SAA_Export-Test-" & Date$ & ".txt"
TableName = "dbo_Employee"
IntRecordsProcessed = 0
'Test that the file exists
If Len(Dir(PathName)) > 0 Then
'Determine if the employee already exists through the ImageId field
stTemp = "SELECT * FROM " & TableName
Set db = CurrentDb()
Set rs = db.OpenRecordset(TableName, , dbOpenDynamic)
Open PathName For Input As #1
While Not EOF(1)
On Error Resume Next
'test output
Open StatusFilePathNametest For Output As #3
'Read a single line (dump the end of line marker)
Line Input #1, stTmp
IntRecordsProcessed = IntRecordsProcessed + 1
'Split the readLine on Comma
Ary = Split(stTmp, "|")
'Look for a matching ImageID
rs.FindFirst "ImageId='" & Ary(7) & "'"
'If there IS a MATCHING ImageID
If Not rs.NoMatch Then
'ImageID DOES exist
If Ary(11) = "TERMINATED" Then 'DELETE the record.
rs.Delete
rs.Requery
Write #3, "Testing Import from SAA, " & IntRecordsProcessed & " records updated"
ElseIf CDbl(rs!SerialNo) <= CDbl(Ary(0)) Then 'It's an update
rs.Edit
rs!SerialNo = Ary(0)
rs!CardNo = Ary(1)
rs!BadgeStatus = Ary(2)
'rs!SSN = IIf(Ary(3) = "", 0, Ary(3))
rs!SSN = Ary(3)
rs!LastName = Ary(4)
rs!FirstName = Ary(5)
rs!Middle = Ary(6)
'rs!ImageID = Ary(7)
rs!ChangeDate = Ary(8)
rs!VendorIndex = Ary(9)
rs!SponsorIndex = Ary(10)
rs!PersonnelStatus = Ary(11)
rs.Update
rs.Requery
Write #3, "Testing Import from SAA, " & IntRecordsProcessed & " records updated"
End If
Else 'ImageID DOES NOT exist,
If Ary(11) <> "TERMINATED" Then
rs.AddNew
rs!SerialNo = Ary(0)
rs!CardNo = Ary(1)
rs!EmployeeID = NextEmpID() 'Call NextEmpID function
rs!BadgeStatus = Ary(2)
'rs!SSN = IIf(Ary(3) = "", 0, Ary(3))
rs!SSN = Ary(3)
rs!LastName = Ary(4)
rs!FirstName = Ary(5)
rs!Middle = Ary(6)
rs!ImageID = Ary(7)
rs!ChangeDate = Ary(8)
rs!VendorIndex = Ary(9)
rs!SponsorIndex = Ary(10)
rs!PersonnelStatus = Ary(11)
rs.Update
rs.Requery
Write #3, "Testing Import from SAA, " & IntRecordsProcessed & " records updated"
End If
End If
Wend
'Write stats to HISTORY File
'Create the File
Open StatusFilePathName For Output As #2
Write #2, "Finished Import from SAA, " & IntRecordsProcessed & " records updated"
Close #2
Close #3
Close #1
rs.Close
Set rs = Nothing
Set db = Nothing
Else 'File does not exist, output blank Status
'Create the File
Open StatusFilePathName For Output As #2
Write #2, "No Export from SAA found, 0 records updated"
Close #2
End If 'File Exists
End Function
Function ImportSAATextFile()
Dim Ary() As String
Dim stTmp As String
Dim rs As dao.Recordset
Dim bFirstNameMatched As Boolean
Dim I As Long
Dim db As Database
Dim PathName As String
Dim TableName As String
Dim stTemp As String
Dim strFirstName As String
Set db = CurrentDb()
'Location of export file from the Sergeant At Arms Database
'PathName = "Z:\SAA_EXPORT.txt"
PathName = "C:\MIKE\ACCESS\OUTPUT\SAA_Export_REM.txt"
TableName = "dbo_Employee"
'Determine if the employee already exists through the CardNo field
'stTemp = "SELECT CardNo FROM " & TableName
stTemp = "SELECT * FROM " & TableName
Set rs = db.OpenRecordset(TableName, , dbOpenDynamic)
Open PathName For Input As #1
While Not EOF(1)
'Read a single line (dump the end of line marker)
Line Input #1, stTmp
'Split the readLine on Comma
Ary = Split(stTmp, "|")
'****If there is not ten fields, skip the record.****
If Ary(2) = "ACTIVE" Then 'Update the record.
rs.FindFirst "CardNo='" & Ary(1) & "'"
'If there is a Matching CardNo
If Not rs.NoMatch Then
'CardNo DOES exist
rs.Edit
rs!SerialNo = Ary(0)
rs!BadgeStatus = Ary(2)
'rs!SSN = IIf(Ary(3) = "", 0, Ary(3))
rs!SSN = Ary(3)
rs!LastName = Ary(4)
rs!FirstName = Ary(5)
rs!Middle = Ary(6)
rs!ImageID = Ary(7)
rs!ChangeDate = Ary(8)
rs!VendorIndex = Ary(9)
rs!SponsorIndex = Ary(10)
rs!PersonnelStatus = Ary(11)
rs.Update
Else 'CardNo DOES NOT exist,
'rs.FindFirst "LastName=" & Ary(4) & "'"
rs.FindFirst "LastName=" & Chr$(34) & Ary(4) & Chr$(34)
If Not rs.NoMatch Then 'LASTNAME matches
If rs!FirstName = Ary(5) Then
'Last and First names match the TEXT file record, UPDATE
rs.Edit
rs!SerialNo = Ary(0)
rs!CardNo = Ary(1)
rs!BadgeStatus = Ary(2)
'rs!SSN = IIf(Ary(3) = "", 0, Ary(3))
rs!SSN = Ary(3)
rs!LastName = Ary(4)
rs!FirstName = Ary(5)
rs!Middle = Ary(6)
rs!ImageID = Ary(7)
rs!ChangeDate = Ary(8)
rs!VendorIndex = Ary(9)
rs!SponsorIndex = Ary(10)
rs!PersonnelStatus = Ary(11)
rs.Update
Else 'FirstName did not match, find record that does.
bFirstNameMatched = False
Do While (Not bFirstNameMatched)
rs.FindNext "LastName=" & Chr$(34) & Ary(4) & Chr$(34)
If Not rs.NoMatch Then 'There is another person with that last name.
If rs!FirstName = Ary(5) Then
'FirstName matches, update record
bFirstNameMatched = True
rs.Edit
rs!SerialNo = Ary(0)
rs!CardNo = Ary(1)
rs!BadgeStatus = Ary(2)
'rs!SSN = IIf(Ary(3) = "", 0, Ary(3))
rs!SSN = Ary(3)
rs!LastName = Ary(4)
rs!FirstName = Ary(5)
rs!Middle = Ary(6)
rs!ImageID = Ary(7)
rs!ChangeDate = Ary(8)
rs!VendorIndex = Ary(9)
rs!SponsorIndex = Ary(10)
rs!PersonnelStatus = Ary(11)
rs.Update
End If
Else 'There is no one else with that last name, ADD Record
'EDIT record.
bFirstNameMatched = True
rs.AddNew
rs!SerialNo = Ary(0)
rs!CardNo = Ary(1)
'Call NextEmpID function
rs!EmployeeID = NextEmpID()
rs!BadgeStatus = Ary(2)
'rs!SSN = IIf(Ary(3) = "", 0, Ary(3))
rs!SSN = Ary(3)
rs!LastName = Ary(4)
rs!FirstName = Ary(5)
rs!Middle = Ary(6)
rs!ImageID = Ary(7)
rs!ChangeDate = Ary(8)
rs!VendorIndex = Ary(9)
rs!SponsorIndex = Ary(10)
rs!PersonnelStatus = Ary(11)
rs.Update
End If
Loop
End If
Else 'There was no matching LastName, Add New Employee Record
rs.AddNew
rs!SerialNo = Ary(0)
rs!CardNo = Ary(1)
rs!EmployeeID = NextEmpID() 'Call NextEmpID function
rs!BadgeStatus = Ary(2)
'rs!SSN = IIf(Ary(3) = "", 0, Ary(3))
rs!SSN = Ary(3)
rs!LastName = Ary(4)
rs!FirstName = Ary(5)
rs!Middle = Ary(6)
rs!ImageID = Ary(7)
rs!ChangeDate = Ary(8)
rs!VendorIndex = Ary(9)
rs!SponsorIndex = Ary(10)
rs!PersonnelStatus = Ary(11)
rs.Update
End If
End If
End If
Wend
Close #1
rs.Close
Set rs = Nothing
Set db = Nothing
End Function
Private Function NextEmpID() As Long
Dim stTmp As String
Dim rsEmpID As dao.Recordset
Dim dbEmpId As Database
Dim PathName As String
Dim TableName As String
Set dbEmpId = CurrentDb()
TableName = "dbo_Employee"
stTmp = "Select Max([EmployeeID]) as MaxEmpID from " & TableName & "'"
Set rsEmpID = dbEmpId.OpenRecordset(stTmp, , dbOpenSnapshot)
NextEmpID = rsEmpID!MaxEmpID + 1
rsEmpID.Close
Set rsEmpID = Nothing
Set dbEmpId = Nothing
End Function
|