Hey guys! I almost raise my white flag with my project, I am a so bad programmer....... My code gives me bugs all the time and I hardly can fix it... Can any of you genius have a look and give me some advise or fix for me...PLEASE...THANKS.....
Please write me if you have any confusion, I will try my best to explain...Thanks
BTW, I think I have huge error on obtaining the field column numbers in Sql (rst.Fields(i)), but I don't know how to fix it... many many people give me help while I was doing this loop, Thanks a lot for all your patient, and I really feel stupid now.......
Code:
Private Sub Zone_slc_AfterUpdate()
Dim db As DAO.Database
Dim rs As DAO.Recordset
Dim AreaArray() As String
Dim Sql1 As String
Dim Sql2 As String
Dim strSql As String
Dim i As Long
Dim j As Long
Dim rst As DAO.Recordset
Dim RecordCnt As Long
strSql = "SELECT [AreaCode],[Zone],[TotalArea],[SmallArea],[SmallerArea],[SmallestArea] FROM AreaQuery1 INNER JOIN AreaQuery1ON AreaQuery2.AreaCode= AreaQuery1.AreaCode;"
Set rst = db.OpenRecordset(strSql, dbOpenSnapshot)
Set db = CurrentDb()
RecordCnt = DCount("AreaCode", "AreaQuery1")
For i = 4 To 6
For j = 0 To RecordCnt
ReDim AreaArray(i, j)
rs.MoveFirst
Do While rst.Fields(i) And Not rs.EOF
Sql1 = "SELECT " & rst.Fields(i) & " From [AreaQuery1] WHERE ( " & rst.Fields(i) & " <>'NA');"
Set rs = db.OpenRecordset(Sql1, dbOpenSnapshot)
If rs.RecordCount > 0 Then
Sql1 = "UPDATE [AreaQuery2]SET[AreaQuery2]." & rst.Fields(i) & " =" & Sql1 & "WHERE [AreaQuery1].[AreaCode]=[AreaQuery2].[AreaCode];"
db.Execute Sql1, dbFailOnError
End If
Set rs = Nothing
Set db = Nothing
Sql2 = "SELECT " & rst.Fields(i) & rst.Fields(i - 1) & [AreaCode] & " FROM [AreaQuery1]WHERE( " & rst.Fields(i) & "='NA');"
Set rs = db.OpenRecordset(Sql2, dbOpenSnapshot)
If rs.RecordCount > 0 Then
Sql2 = "SELECT Count([AreaQuery1]." & rst.Fields(i - 1) & ") AS CountOfArea, AreaQuery2." & rst.Fields(i - 1) & " " & _
"FROM AreaQuery2 INNER JOIN AreaQuery1 ON AreaQuery2.AreaCode= AreaQuery1.AreaCode" & _
"GROUP BY AreaQuery2." & rst.Fields(i - 1) & ";"
db.Execute Sql2, dbfilonerror
If ALLCountOfArea = 1 Then
Sql2 = "SELECT " & rst.Fields(i - 1) & " FROM AreaQuery1INNER JOIN AreaQuery2 WHERE ( " & rst.Fields(i) & " ='NA');"
Set rs = db.OpenRecordset(Sql2, dbOpenSnapshot)
Sql2 = "UPDATE [AreaQuery2] SET [AreaQuery2]." & rst.Fields(i) & "=" & Sql2 & " WHERE [AreaQuery1].[AreaCode]=[AreaQuery2].[AreaCode];"
db.Execute Sql2, dbFailOnError
Else
Select Case CountOfArea
Case CountOfArea = 1
Sql2 = "SELECT " & rst.Fields(i - 1) & " FROM AreaQuery1INNER JOIN AreaQuery2 WHERE ( " & rst.Fields(i) & " ='NA');"
Set rs = db.OpenRecordset(Sql2, dbOpenSnapshot)
Sql2 = "Update [AreaQuery2]SET[AreaQuery2]." & rst.Fields(i) & " = [AreaQuery1]." & rst.Fields(i - 1) & "WHERE [AreaQuery1].[AreaCode]=[AreaQuery2].[AreaCode];"
db.Execute Sql2, dbFailOnError
Case CountOfArea > 1
Sql2 = "SELECT " & rst.Fields(i - 1) & " FROM AreaQuery1INNER JOIN AreaQuery2 WHERE ( " & rst.Fields(i) & " ='NA');"
Set rs = db.OpenRecordset(Sql2, dbOpenSnapshot)
Sql2 = "Update [AreaQuery2] SET [AreaQuery2]." & rst.Fields(i) & "=[AreaQuery1]." & rst.Fields(i - 1) & " WHERE [AreaQuery1].[AreaCode]=[AreaQuery2].[AreaCode];"
db.Execute Sql2, dbFailOnError
End Select
End If
End If
rs.MoveNext
Loop
Next j
Next i
End Sub
Please write me if you have any confusion, I will try my best to explain...Thanks
BTW, I think I have huge error on obtaining the field column numbers in Sql (rst.Fields(i)), but I don't know how to fix it... many many people give me help while I was doing this loop, Thanks a lot for all your patient, and I really feel stupid now.......
Last edited: