Looping Code is not moving through the form - vba

I have a form in my database that pulls data from a query to calculate the subassembly parts needed on a weekly basis and with the click of the "complete" button the required components should be moved into and out of inventory yet nothing happens when the complete button is clicked. The code should loop through and move all the parts but nothing happens.
I have stepped through to see if there are any errors and corrected a few syntax errors but that is all I have done.
Private Sub Command96_Click()
Dim ctl As Control
Dim ctln
Dim Qty As Double
Dim db As DAO.Database
Set db = CurrentDb
Dim rs As DAO.Recordset
For Each ctl In Me.Controls
Select Case TypeName(ctl)
Case "TextBox"
Select Case ctl.ControlName
Case ctl Like "*Q"
ctln = Me.Controls(Right(ctl, Len(ctl) - 1))
If Not IsNull(DLookup("[In]", "[Inventory]", "[PartNum] = '" & ctln & "'AND [YearNum] = " & Me.YearNum & " AND [WeekNum] = " & Me.WeekNum & "")) Then
num = DLookup("[In]", "[Inventory]", "[PartNum] = '" & ctln & "' AND [YearNum] = " & Me.YearNum & " AND [WeekNum] = " & Me.WeekNum & "") + ctl
Else
num = ctl
End If
If Not IsNull(DLookup("[PartNum]", "[Inventory]", "[PartNum] = '" & ctln & "'AND [YearNum] = " & Me.YearNum & " AND [WeekNum] = " & Me.WeekNum & "")) Then
CurrentDb.Execute "UPDATE [Inventory] " _
& "SET [In] = " & num & " " _
& "WHERE [PartNum] = '" & ctln & "'AND [YearNum] = " & Me.YearNum & " AND [WeekNum] = " & Me.WeekNum & "", dbFailOnError
Else
CurrentDb.Execute "INSERT INTO [Inventory] " _
& "VALUES ('" & ctln & "'," & Me.YearNum & "," & Me.WeekNum & "," & num & ",0)", dbFailOnError
End If
num = 0
Set rs = db.OpenRecordset("SELECT UsedPartNum, (Quantity * " & ctl & ") AS Used FROM SubPartsUsed WHERE FinPartNum = '" & PartNum & "'", dbOpenDynaset)
If Not (rs.EOF And rs.BOF) Then
rs.MoveFirst
Do Until rs.EOF = True
If Not IsNull(DLookup("[Out]", "[Inventory]", "[PartNum] = '" & rs!UsedPartNum & "'AND [YearNum] = " & Me.YearNum & " AND [WeekNum] = " & Me.WeekNum & "")) Then
num = DLookup("[Out]", "[Inventory]", "[PartNum] = '" & rs!UsedPartNum & "' AND [YearNum] = " & Me.YearNum & " AND [WeekNum] = " & Me.WeekNum & "") + rs!Used
Else
num = rs!Used
End If
If Not IsNull(DLookup("[PartNum]", "[Inventory]", "[PartNum] = '" & rs!UsedPartNum & "'AND [YearNum] = " & Me.YearNum & " AND [WeekNum] = " & Me.WeekNum & "")) Then
CurrentDb.Execute "UPDATE [Inventory] " _
& "SET [Out] = " & num & " " _
& "WHERE [PartNum] = '" & rs!UsedPartNum & "'AND [YearNum] = " & Me.YearNum & " AND [WeekNum] = " & Me.WeekNum & ""
Else
CurrentDb.Execute "INSERT INTO [Inventory] " _
& "VALUES ('" & rs!UsedPartNum & "'," & Me.YearNum & "," & Me.WeekNum & ",0," & num & ")"
End If
rs.MoveNext
Loop
End If
rs.Close
Set rs = Nothing
End Select
End Select
I expect the parts to be entered into inventory as complete subassembly parts and the components to make them should be removed from inventory.

Code is done by high professional specialist in VBA, many shortcuts for code executing. It could be difficult for new VBA programmer to correct this code, so I think:
first step should be adding Debug.Print code executed here at line number XXX in order to study what lines are executed, and if their execution is done as assumpted.
After that, if there is OK with code logic, Debug.Print all SQL statements, that are generated. So you can check their correctness through executing in query designer
E.g.:
Private Sub Command96_Click()
Dim ctl As Control
Dim ctln
Dim Qty As Double
Dim db As DAO.Database
Set db = CurrentDb
Dim rs As DAO.Recordset
Dim sSQL As String
For Each ctl In Me.Controls
Select Case TypeName(ctl)
Debug.Pring "looping through controls"
Case "TextBox"
Select Case ctl.ControlName
Case ctl Like "*Q"
Debug.Pring "Control with Q letter is found"
ctln = Me.Controls(Right(ctl, Len(ctl) - 1))
If Not IsNull(DLookup("[In]", "[Inventory]", "[PartNum] = '" & ctln & "'AND [YearNum] = " & Me.YearNum & " AND [WeekNum] = " & Me.WeekNum & "")) Then
Debug.Print "Num is DLookuped"
num = DLookup("[In]", "[Inventory]", "[PartNum] = '" & ctln & "' AND [YearNum] = " & Me.YearNum & " AND [WeekNum] = " & Me.WeekNum & "") + ctl
Else
num = ctl
End If
If Not IsNull(DLookup("[PartNum]", "[Inventory]", "[PartNum] = '" & ctln & "'AND [YearNum] = " & Me.YearNum & " AND [WeekNum] = " & Me.WeekNum & "")) Then
Debug.Print "Executing Update Query for not null dlookup"
sSQL = "UPDATE [Inventory] " _
& "SET [In] = " & num & " " _
& "WHERE [PartNum] = '" & ctln & "'AND [YearNum] = " & Me.YearNum & " AND [WeekNum] = " & Me.WeekNum & ""
Debug.Print sSQL
CurrentDb.Execute sSQL, dbFailOnError
Else
Debug.Print "Executing Update Query for null dlookup"
sSQL = "INSERT INTO [Inventory] " _
& "VALUES ('" & ctln & "'," & Me.YearNum & "," & Me.WeekNum & "," & num & ",0)"
Debug.Print sSQL
CurrentDb.Execute sSQL, dbFailOnError
End If
num = 0
Set rs = db.OpenRecordset("SELECT UsedPartNum, (Quantity * " & ctl & ") AS Used FROM SubPartsUsed WHERE FinPartNum = '" & PartNum & "'", dbOpenDynaset)
If Not (rs.EOF And rs.BOF) Then
Debug.Print "Beginning action for each record in PartNum select query"
rs.MoveFirst
Do Until rs.EOF = True
If Not IsNull(DLookup("[Out]", "[Inventory]", "[PartNum] = '" & rs!UsedPartNum & "'AND [YearNum] = " & Me.YearNum & " AND [WeekNum] = " & Me.WeekNum & "")) Then
Debug.Print "Executing Dlookup for element in PartNum select query"
num = DLookup("[Out]", "[Inventory]", "[PartNum] = '" & rs!UsedPartNum & "' AND [YearNum] = " & Me.YearNum & " AND [WeekNum] = " & Me.WeekNum & "") + rs!Used
Else
num = rs!Used
End If
If Not IsNull(DLookup("[PartNum]", "[Inventory]", "[PartNum] = '" & rs!UsedPartNum & "'AND [YearNum] = " & Me.YearNum & " AND [WeekNum] = " & Me.WeekNum & "")) Then
Debug.Print "Executing Update for not null DLookup element in PartNum select query"
sSQL = "UPDATE [Inventory] " _
& "SET [Out] = " & num & " " _
& "WHERE [PartNum] = '" & rs!UsedPartNum & "'AND [YearNum] = " & Me.YearNum & " AND [WeekNum] = " & Me.WeekNum & ""
Debug.Print sSQL
CurrentDb.Execute sSQL
Else
Debug.Print "Executing Update for null DLookup element in PartNum select query"
sSQL = "INSERT INTO [Inventory] " _
& "VALUES ('" & rs!UsedPartNum & "'," & Me.YearNum & "," & Me.WeekNum & ",0," & num & ")"
Debug.Print sSQL
CurrentDb.Execute sSQL
End If
rs.MoveNext
Loop
End If
rs.Close
Set rs = Nothing
End Select
End Select
In this case you should study your Immediate windows (openes by Ctrl + G) to see, what is the execution plan, and what SQL texts are generated, and check all of them.
Otherwise, there is too many your business specific logics in this code, and it is quite impossible to understand program behavior. Maybe such behavior is assumpted, due to business logics? Many, many questions

Hit the F9 key over, and over, and over, until you see what the problem is. Also, leverage 'Add Watch' to see what values are passed to which variables. That should help immensely. Finally, if this is done by a professional, why are you using: 'Command96_Click()'? Of course that's not the problem, but it's not helping either.

Related

UPDATE Query in MS Access using SQL in VBA with declared strings

been a while since I've posted so please forgive any formatting issues. Looking to update a table field with a value from another record in the same field in the same table.
I've declared two strings, and the strDeal string is coming back with the correct values when debugging. However when I introduce the string into the sql statement I can't the query to update the field. I'm not sure exactly what isn't working correctly, so any help would be appreciated.
The basics are I'm trying to update the Case_Qty field with a value from the same field in the same table based on the returned value from a subquery. Thanks!
Dim strDeal As String
Dim strSQL As String
strDeal = DMax("[Deal_No]", "[tblStructuresNoDAworking]", "[Structure_Name] = Forms!frmStructNoDASetup!Structure_Name AND [FG_Ind] = 0 AND [Deal_No] < Forms!frmStructNoDASetup!Deal_No")
strSQL = "UPDATE tblStructuresNoDAworking SET tblStructuresNoDAworking.Case_Qty = (SELECT [Case_Qty] FROM tblStructuresNoDAworking WHERE [Structure_Name] = '" & Me.Structure_Name.Value & "' AND [Deal_No] = '" & strDeal & "') WHERE [Structure_Name] = '" & Me.Structure_Name.Value & "' AND [Deal_No] = '" & Me.Deal_No.Value & "'"
DoCmd.RunSQL (strSQL)
Try this where you concatenate the values from the form:
Dim strDeal As String
Dim strSQL As String
strDeal = DMax("[Deal_No]", "[tblStructuresNoDAworking]", "[Structure_Name] = '" & Forms!frmStructNoDASetup!Structure_Name & "' AND [FG_Ind] = 0 AND [Deal_No] < '" & Forms!frmStructNoDASetup!Deal_No & "'")
strSQL = "UPDATE tblStructuresNoDAworking " & _
"SET tblStructuresNoDAworking.Case_Qty = " & _
" (SELECT [Case_Qty] " & _
" FROM tblStructuresNoDAworking " & _
" WHERE [Structure_Name] = '" & Me.Structure_Name.Value & "' AND [Deal_No] = '" & strDeal & "') " & _
"WHERE [Structure_Name] = '" & Me.Structure_Name.Value & "' AND [Deal_No] = '" & Me.Deal_No.Value & "'"
DoCmd.RunSQL strSQL
For numeric Deal:
Dim Deal As Long
Dim strSQL As String
Deal = DMax("[Deal_No]", "[tblStructuresNoDAworking]", "[Structure_Name] = '" & Forms!frmStructNoDASetup!Structure_Name & "' AND [FG_Ind] = 0 AND [Deal_No] < '" & Forms!frmStructNoDASetup!Deal_No & "'")
strSQL = "UPDATE tblStructuresNoDAworking " & _
"SET tblStructuresNoDAworking.Case_Qty = " & _
" (SELECT [Case_Qty] " & _
" FROM tblStructuresNoDAworking " & _
" WHERE [Structure_Name] = '" & Me.Structure_Name.Value & "' AND [Deal_No] = " & Deal & ") " & _
"WHERE [Structure_Name] = '" & Me.Structure_Name.Value & "' AND [Deal_No] = " & Me.Deal_No.Value & ""
DoCmd.RunSQL strSQL

VBA error matching date from table and date from query when date has leading zero eg 01/04 or 02/04

I have a query in MS Access called qryRMCountStudentsBySessionWP. The results are shown below.
I have some VBA code shown below. Basically if the Timetable_Date, Timetable_Session and Location match Exam_Date, Exam_Session and Exam_Location of a record in a table called Invigilation, then changes are made to that record's start time.
This works fabulously for 30/03/2020 and 31/03/2020 but not any of the April dates. I think this is because of leading zeros but I just can't figure out how to fix it. Any ideas?
Private Sub wordProcessing()
Dim dbs As Database
Dim name As String
Dim SQL As String
Dim rstAllSessions As Recordset
Set dbs = CurrentDb
SQL = "SELECT Timetable_Date, Timetable_Session, Location, CountOfStudent_Ref FROM qryRMCountStudentsBySessionWP;"
Set rstAllSessions = dbs.OpenRecordset(SQL)
rstAllSessions.MoveFirst
Do Until rstAllSessions.EOF ' for each sesson update invigilation table
If rstAllSessions.Fields(3) >= 10 Then
If rstAllSessions.Fields(1) = "A" Then
dbs.Execute "UPDATE Invigilation SET Start_Time = TimeValue('08:30:00'), Notes = 'WP Exam with 10 or more students' WHERE Invigilation.Exam_Date = #" & rstAllSessions!Timetable_Date & "# AND Invigilation.Exam_Location = " & "'" & rstAllSessions!Location & "'" & "AND Invigilation.Exam_Session = " & "'" & rstAllSessions!Timetable_Session & "'" & ";", dbFailOnError
Else
dbs.Execute "UPDATE Invigilation SET Start_Time = TimeValue('13:00:00'), Notes = 'WP Exam with 10 or more students' WHERE Invigilation.Exam_Date = #" & rstAllSessions!Timetable_Date & "# AND Invigilation.Exam_Location = " & "'" & rstAllSessions!Location & "'" & "AND Invigilation.Exam_Session = " & "'" & rstAllSessions!Timetable_Session & "'" & ";", dbFailOnError
End If
ElseIf rstAllSessions.Fields(3) >= 5 Then
If rstAllSessions.Fields(1) = "A" Then
dbs.Execute "UPDATE Invigilation SET Start_Time = TimeValue('08:35:00'), Notes = 'WP Exam with 5 or more students' WHERE Invigilation.Exam_Date = #" & rstAllSessions!Timetable_Date & "# AND Invigilation.Exam_Location = " & "'" & rstAllSessions!Location & "'" & "AND Invigilation.Exam_Session = " & "'" & rstAllSessions!Timetable_Session & "'" & ";", dbFailOnError
Else
dbs.Execute "UPDATE Invigilation SET Start_Time = TimeValue('13:05:00'), Notes = 'WP Exam with 5 or more students' WHERE Invigilation.Exam_Date = #" & rstAllSessions!Timetable_Date & "# AND Invigilation.Exam_Location = " & "'" & rstAllSessions!Location & "'" & "AND Invigilation.Exam_Session = " & "'" & rstAllSessions!Timetable_Session & "'" & ";", dbFailOnError
End If
ElseIf rstAllSessions.Fields(3) > 1 Then
If rstAllSessions.Fields(1) = "A" Then
dbs.Execute "UPDATE Invigilation SET Start_Time = TimeValue('08:40:00'), Notes = 'WP Exam with >1 and <5 students' WHERE Invigilation.Exam_Date = #" & rstAllSessions!Timetable_Date & "# AND Invigilation.Exam_Location = " & "'" & rstAllSessions!Location & "'" & "AND Invigilation.Exam_Session = " & "'" & rstAllSessions!Timetable_Session & "'" & ";", dbFailOnError
Else
dbs.Execute "UPDATE Invigilation SET Start_Time = TimeValue('13:10:00'), Notes = 'WP Exam with >1 and <5 students' WHERE Invigilation.Exam_Date = #" & rstAllSessions!Timetable_Date & "# AND Invigilation.Exam_Location = " & "'" & rstAllSessions!Location & "'" & "AND Invigilation.Exam_Session = " & "'" & rstAllSessions!Timetable_Session & "'" & ";", dbFailOnError
End If
Else
If rstAllSessions.Fields(1) = "A" Then
dbs.Execute "UPDATE Invigilation SET Start_Time = TimeValue('08:45:00'), Notes = 'WP Exam with 1 student' WHERE Invigilation.Exam_Date = #" & rstAllSessions!Timetable_Date & "# AND Invigilation.Exam_Location = " & "'" & rstAllSessions!Location & "'" & "AND Invigilation.Exam_Session = " & "'" & rstAllSessions!Timetable_Session & "'" & ";", dbFailOnError
Else
dbs.Execute "UPDATE Invigilation SET Start_Time = TimeValue('13:15:00'), Notes = 'WP Exam with 1 student' WHERE Invigilation.Exam_Date = #" & rstAllSessions!Timetable_Date & "# AND Invigilation.Exam_Location = " & "'" & rstAllSessions!Location & "'" & "AND Invigilation.Exam_Session = " & "'" & rstAllSessions!Timetable_Session & "'" & ";", dbFailOnError
End If
End If
' doesn't do dates with leading 0s?????
rstAllSessions.MoveNext
Loop
rstAllSessions.Close
dbs.Close
End Sub
Date values are numeric, thus no leading zeroes. But you must pass properly formatted string expressions for the date values when concatenating:
"UPDATE Invigilation SET Start_Time = TimeSerial(13,15,0), Notes = 'WP Exam with 1 student' WHERE Invigilation.Exam_Date = #" & Format(rstAllSessions!Timetable_Date, "yyyy\/mm\/dd") & "# AND Invigilation.Exam_Location = '" & rstAllSessions!Location & "' AND Invigilation.Exam_Session = '" & rstAllSessions!Timetable_Session & "';"

Can I convert my query to use select .. like .. or?

Can the query in the code below be converted to Select / Like / Or ?
Private Sub cmdQDef_Click()
Dim qd As DAO.QueryDef, db As DAO.Database
Dim ssql As String, WhereName As String, WhereTitle As String
Set db = CurrentDb
If Me.FilterName & "" = "" Then
DoCmd.OpenQuery "q_Search_qdef"
Exit Sub
Else
End If
ssql = "Select * From Employees"
Set qd = db.QueryDefs("q_Search_qdef")
WhereName = "'" & Replace(Me.FilterName, ",", "','") & "'"
WhereTitle = "'" & Replace(Me.FilterTitle, ",", "','") & "'"
ssql = ssql & " Where [First name] In(" & WhereName & ")AND " & _
"[Job Title] In (" & WhereTitle & ")"
qd.SQL = ssql
DoCmd.OpenQuery "q_Search_qdef"
End Sub
Yes:
WhereName = "'*" & Replace(Me.FilterName, ",", "','") & "*'"
WhereTitle = "'" & Replace(Me.FilterTitle, ",", "','") & "'"
ssql = ssql & " Where ([First name] Like " & WhereName & ") OR " & _
"([Job Title] In (" & WhereTitle & ")"

Syntax error in UPDATE statement. via cmd.nonexecutequery

If Me.TextBox1.Tag & "" = "" Then
cmd.CommandText = "INSERT INTO Table1(CandidateID, Fname, Mname, Lname, Partylist, Pst, course) " & _
" VALUES (" & Me.TextBox1.Text & ", '" & Me.TextBox2.Text & "', '" & Me.TextBox3.Text & "', '" & _
Me.TextBox4.Text & "', '" & Me.ComboBox1.Text & "', '" & Me.ComboBox2.Text & "', '" & Me.ComboBox3.Text & "')"
cmd.ExecuteNonQuery()
Else
cmd.CommandText = "UPDATE table " & _
" SET CandidateID=" & Me.TextBox1.Text & _
", Fname='" & Me.TextBox2.Text & "'" & _
", Mname='" & Me.TextBox3.Text & "'" & _
", Lname='" & Me.TextBox4.Text & "'" & _
", Partylist='" & Me.ComboBox1.Text & "'" & _
", Pst='" & Me.ComboBox2.Text & "'" & _
", Course='" & Me.ComboBox3.Text & "'" & _
" WHERE CandidateID=" & Me.TextBox1.Tag
cmd.ExecuteNonQuery()
cmd.CommandText = "UPDATE Table1 " & _
" SET CandidateID=" & Me.TextBox1.Text & _
", Fname='" & Me.TextBox2.Text & "'" & _
", Mname='" & Me.TextBox3.Text & "'" & _
", Lname='" & Me.TextBox4.Text & "'" & _
", Partylist='" & Me.ComboBox1.Text & "'" & _
", Pst='" & Me.ComboBox2.Text & "'" & _
", Course='" & Me.ComboBox3.Text & "'" & _
" WHERE CandidateID=" & Me.TextBox1.Tag
cmd.ExecuteNonQuery()
my table was incorrect.
thank you for those who replied to my thread

open a query based on many choices

hello i wrote this code to open a report based on a query
and this query is based on the toggle button
the problem is if i press one toggle button all work
but if i press more than toggle button in the same time it will not give me the right records or it will give me an empty report
this is the code
DoCmd.SetWarnings False
DoCmd.RunSQL "DELETE * FROM tbl_Mouzakarat"
Dim p_7abes As String
Dim p_gharame As String
Dim p_done As String
Dim p_undone As String
Dim p_khoulasa As String
Dim p_mouzakara As String
Dim p_karar As String
Dim p_jaze2e As String
Dim p_lebanese As String
Dim p_foreign As String
Dim p_SQL_criteria As String
p_7abes = Trim(Me!text_7abes & " ")
p_gharame = Trim(Me!text_gharame & " ")
p_done = Trim(Me!text_done & " ")
p_undone = Trim(Me!text_undone & " ")
p_khoulasa = Trim(Me!text_khoulasa & " ")
p_mouzakara = Trim(Me!text_mouzakara & " ")
p_karar = Trim(Me!text_karar & " ")
p_jaze2e = Trim(Me!text_jaze2e & " ")
p_lebanese = Trim(Me!text_lebanese & " ")
p_foreign = Trim(Me!text_lebanese & " ")
If p_7abes <> "" Then
p_SQL_criteria = "[Punish]" & " LIKE '" & p_7abes & "'"
End If
If p_gharame <> "" Then
p_SQL_criteria = "[Punish]" & " LIKE '*" & p_gharame & "*'"
End If
If p_done <> "" Then
p_SQL_criteria = "[Status_Check]" & " LIKE '*" & p_done & "*'"
End If
If p_undone <> "" Then
p_SQL_criteria = "[Status_Check]" & " LIKE '*" & p_undone & "*'"
End If
If p_khoulasa <> "" Then
p_SQL_criteria = "[Type]" & " LIKE '*" & p_khoulasa & "*'"
End If
If p_mouzakara <> "" Then
p_SQL_criteria = "[Type]" & " LIKE '*" & p_mouzakara & "*'"
End If
If p_karar <> "" Then
p_SQL_criteria = "[Type]" & " LIKE '*" & p_karar & "*'"
End If
If p_jaze2e <> "" Then
p_SQL_criteria = "[Type]" & " LIKE '*" & p_jaze2e & "*'"
End If
If p_lebanese <> "" Then
p_SQL_criteria = "[Nationality]" & " LIKE '*" & p_lebanese & "*'"
End If
If p_foreign <> "" Then
p_SQL_criteria = "[Nationality]" & " NOT LIKE '*" & p_foreign & "*'"
End If
If Me.chk7abes.Value = True Then
DoCmd.RunSQL "INSERT INTO tbl_Mouzakarat select * from " & "[qry_Mouzakarat]" & " where " & p_SQL_criteria
End If
If Me.chkGharame.Value = True Then
DoCmd.RunSQL "INSERT INTO tbl_Mouzakarat select * from " & "[qry_Mouzakarat]" & " where " & p_SQL_criteria
End If
If Me.chkDone.Value = True Then
DoCmd.RunSQL "INSERT INTO tbl_Mouzakarat select * from " & "[qry_Mouzakarat]" & " where " & p_SQL_criteria
End If
If Me.ChkUndone.Value = True Then
DoCmd.RunSQL "INSERT INTO tbl_Mouzakarat select * from " & "[qry_Mouzakarat]" & " where " & p_SQL_criteria
End If
If Me.chkKhoulasa.Value = True Then
DoCmd.RunSQL "INSERT INTO tbl_Mouzakarat select * from " & "[qry_Mouzakarat]" & " where " & p_SQL_criteria
End If
If Me.chkMouzakara.Value = True Then
DoCmd.RunSQL "INSERT INTO tbl_Mouzakarat select * from " & "[qry_Mouzakarat]" & " where " & p_SQL_criteria
End If
If Me.chkKarar7abes.Value = True Then
DoCmd.RunSQL "INSERT INTO tbl_Mouzakarat select * from " & "[qry_Mouzakarat]" & " where " & p_SQL_criteria
End If
If Me.chkKararJaze2e.Value = True Then
DoCmd.RunSQL "INSERT INTO tbl_Mouzakarat select * from " & "[qry_Mouzakarat]" & " where " & p_SQL_criteria
End If
DoCmd.OpenReport "rpt_Mouzakarat", acViewPreview
DoCmd.Close acForm, "frm_Printing"
will someone check the code please
You are overwriting p_SQL_criteria each time then running the same exact query each time no matter how many toggle buttons are pressed.
"INSERT INTO tbl_Mouzakarat select * from " & "[qry_Mouzakarat]" & " where " & p_SQL_criteria
This is always the exact same SQL statement for each of your DoCmd statements.
I'm not really sure how your toggle buttons interact with the parameters you are trying to create so it's hard to suggest solutions, but this is why your having the problem you are having.