اذهب الي المحتوي
أوفيسنا
بحث مخصص من جوجل فى أوفيسنا
Custom Search

مطلوب تحديث حقل في جدول بناء على مصدر بيانات مربع تحرير


ابوخليل
إذهب إلى أفضل إجابة Solved by ابو جودي,

الردود الموصى بها

السلام عليكم ورحمة الله وبركاته

في المرفق ادناه جدول يحتوي على حقل واحد والقيم الموجودة فيه عبارة عن ارقام

ونموذج يشتمل على مربع تحرير  قيمته رقمية والعمود الثاني نص

المطلوب تحديث الحقول في الجدول بمعرفة القيمة الرقمية بينهما الى القيمة النصية الموجودة في مربع التحرير والتي تمثل العمود الثاني

اي الرقم 1 في الجدول يتحدث الى كلمة "الأبيض" والرقم 2 الى الأزرق  وهكذا .....

حسب الشروط التالية :

1- يكون التحديث بواسطة مصدر بيانات مربع التحرير وهو الاستعلام الفرعي داخله وليس من الجدول  tblSp مباشرة .. فالاخيرة هذه اعرفها ولا تخدمني

2- يتم التحديث على الجميع جملة مرة واحدة .. بمعنى اني لا اريد ان اختار القيمة من مربع التحرير ثم اقوم بالتحديث .. ايضا هذه اعرفها ولا تخدمني

يمكن ان يتساءل احد ولماذا هذا التكلف .. والجواب ان المعيار الموجود في الجدول عندي هو أسماء نماذج وأسماء مربعات تحرير ولا يمكن التحديث الا من خلالها

شاكرا مقدما لكل من يحاول تنفيذ الفكرة

 

DatabaseUp.rar

رابط هذا التعليق
شارك

وعليكم السلام ورحمة الله تعالى وبركاته
هل يفى ذلك بالغرض استاذى الجليل ومعلمى القدير و والدى الحبيب 

Dim db As DAO.Database
Dim strSQL As String
Dim newValue As String
Dim IDValue As Integer

' Open the database
Set db = CurrentDb

' Get the text and numeric value from the combo box
newValue = Me.Combo0.Column(1)
IDValue = Me.Combo0.Value

' Check for the existence of a value before inserting it into the query
If Not IsNull(newValue) Then
    ' Update the table using the text value
    strSQL = "UPDATE Table1 " & _
             "SET text1 = '" & Nz(newValue, "") & "' " & _
             "WHERE Table1.text1 = '" & IDValue & "';"

    ' Execute the query
    db.Execute strSQL
Else
    ' Notification or other action if no value is found
    MsgBox "No matching value found in tblSp.", vbExclamation
End If

' Close the database
Set db = Nothing

 

رابط هذا التعليق
شارك

2 ساعات مضت, ابوخليل said:

2- يتم التحديث على الجميع جملة مرة واحدة .. بمعنى اني لا اريد ان اختار القيمة من مربع التحرير ثم اقوم بالتحديث

وبعد ان انتبهت لهذه النقطة

هذه فكرتى

 


Dim db As DAO.Database
Dim strSQL As String
Dim intcomboValue As Integer
Dim strcomboValue As String
Dim rsCombo As DAO.Recordset

' Open the database
Set db = CurrentDb

' Open a recordset for the values in Combo0
Set rsCombo = db.OpenRecordset("SELECT tblSp.id, tblSp.sName, * FROM tblSp;", dbOpenSnapshot)

' Disable the error handling temporarily
On Error Resume Next

' Loop through each record in Combo0
Do Until rsCombo.EOF
    ' Get the value from the current record in Combo0
    intcomboValue = rsCombo.Fields("id").Value
    strcomboValue = rsCombo.Fields("sName").Value
    
    ' Construct the SQL update statement
    strSQL = "UPDATE Table1 " & _
             "SET text1 = '" & Nz(strcomboValue, "") & "' " & _
             "WHERE Table1.text1 = '" & intcomboValue & "';"

    ' Execute the query
    db.Execute strSQL

    ' Check for errors
    If Err.Number <> 0 Then
        ' Clear the error
        Err.Clear
        ' Move to the next record without processing the error
        Resume Next
    End If

    ' Move to the next record in Combo0
    rsCombo.MoveNext
Loop

' Enable the normal error handling
On Error GoTo 0

' Close the recordset and the database
rsCombo.Close
Set rsCombo = Nothing
Set db = Nothing

 

انا اعتبرت مربع السرد غير موجود على النموذج اصلا
ووضعت مصدر البيانات فى الكود بديلا عنه
 

Set rsCombo = db.OpenRecordset("SELECT tblSp.id, tblSp.sName, * FROM tblSp;", dbOpenSnapshot)

مش عارف ده صح واللا غلط او بالاخص
مش عارف انا فاهم واللا مش فاهم انت عارف فهمى على اد حالى ثغنون :biggrin:

 

 

 

DatabaseUp2.rar

  • Like 1
  • Haha 1
رابط هذا التعليق
شارك

26 دقائق مضت, ابو جودي said:

انت عارف فهمى على اد حالى ثغنون :biggrin:

لا ابدا ... انا شدتني هذه العبارة .... دائم اسلوبك جذاب كاخلاقك النبيلة ونقاء قلبك 

بارك الله فيك اخي الكريم محمد .. ونتعلم منك الاخلاق الكريمة ... :fff:

  • Like 1
  • Thanks 1
رابط هذا التعليق
شارك

شكرا اخي الغالي ابو جودي 

الكود الأول قربت من الحل

اعتقد المسألة بحاجة الى ما يشبه combo.Recordset  كجملة استعلام  نتعامل معها ولكن لا يتوفر الا combo.RowSurce

لا اريد جدول tblSp يدخل في العملية 

صحيح  ان الجدول هو مصدر البيانات ولكن اريد اتعامل مع هذه البيانات من خلال النموذج ومربع التحرير

وقد ذكرت السبب هو ان السجل في الجدول الاصلي الهدف لا يمكن تحديثه بطريقة صحيحة الا بدلالة النموذج ومربع التحرير

بمعنى اوضح : اريد استخراج مصدر بيانات مربع التحرير  الذي كما هو معلوم الاستعلام الداخلي

 

رابط هذا التعليق
شارك

منذ ساعه, Barna said:

لا ابدا ... انا شدتني هذه العبارة .... دائم اسلوبك جذاب كاخلاقك النبيلة ونقاء قلبك 

بارك الله فيك اخي الكريم محمد .. ونتعلم منك الاخلاق الكريمة ... :fff:

العفو منكم استاذى الجليل ومعلمى القدير


 

منذ ساعه, ابوخليل said:

اعتقد المسألة بحاجة الى ما يشبه combo.Recordset  كجملة استعلام  نتعامل معها ولكن لا يتوفر الا combo.RowSurce

 


طيب ممكن نعمل الكود الثانى بالشكل الاتى ليتم التحديث على الجميع جملة مرة واحدة وبدون اختيار اى قيمة من مربع التحرير
ولكن سوف يكون ذلك التحديث ب
الاعتماد على القيم من مربع السرد مهما كان مصدر بياناته

 


Dim db As DAO.Database
Dim strSQL As String
Dim intcomboValue As Integer
Dim strcomboValue As String
Dim rsCombo As DAO.Recordset

' Open the database
Set db = CurrentDb

' Open a recordset for the values in Combo0
Set rsCombo = db.OpenRecordset(Me.Combo0.RowSource, dbOpenSnapshot)

' Disable the error handling temporarily
On Error Resume Next

' Loop through each record in Combo0
Do Until rsCombo.EOF
    ' Get the value from the current record in Combo0
    intcomboValue = rsCombo.Fields("id").Value
    strcomboValue = rsCombo.Fields("sName").Value
    
    ' Construct the SQL update statement
    strSQL = "UPDATE Table1 " & _
             "SET text1 = '" & Nz(strcomboValue, "") & "' " & _
             "WHERE Table1.text1 = '" & intcomboValue & "';"

    ' Execute the query
    db.Execute strSQL

    ' Check for errors
    If Err.Number <> 0 Then
        ' Clear the error
        Err.Clear
        ' Move to the next record without processing the error
        Resume Next
    End If

    ' Move to the next record in Combo0
    rsCombo.MoveNext
Loop

' Enable the normal error handling
On Error GoTo 0

' Close the recordset and the database
rsCombo.Close
Set rsCombo = Nothing
Set db = Nothing

 

الان الاعتماد على مربع السرد مهما كان مصدر بياناته من خلال السطر
 

Set rsCombo = db.OpenRecordset(Me.Combo0.RowSource, dbOpenSnapshot)

 

  • Like 1
  • Thanks 2
رابط هذا التعليق
شارك

نعم هو كذا  .. تسلم الأنامل التي سطرت هذا الابداع

الف شكر استاذنا .. :fff:

هذا الكود هو حجر الاساس .. سوف اطوره ليشمل جميع مربعات التحرير داخل النموذج

لن استغني عن مساعدتك ولمساتك ان احتجت لذلك .

  • Thanks 1
رابط هذا التعليق
شارك

6 دقائق مضت, ابوخليل said:

نعم هو كذا  .. تسلم الأنامل التي سطرت هذا الابداع

الف شكر استاذنا .. :fff:

هذا الكود هو حجر الاساس .. سوف اطوره ليشمل جميع مربعات التحرير داخل النموذج

لن استغني عن مساعدتك ولمساتك ان احتجت لذلك .

العفو منكم استاذى الجليل ومعلمى القدير و والدى الحبيب
انا وكل ما أملك ملك يمينك وتحت امرك وهذه بضاعتكم وترد اليكم
ما انا الا طويلب العلم الذى يتعلم منكم وعلى ايديكم وكل الفضل يعود لكم بعد رب العزة سبحانه وتعالى

12 دقائق مضت, ابوخليل said:

اطوره ليشمل جميع مربعات التحرير داخل النموذج

ممكن عمل روتين عام ليتم استدعاءه من خلال optional
وذلك لارجاع اسم مربع النص الى الروتين كى لا تتم كتابة الاكواد كثيرا ان تعددت مربعات النص

  • Like 2
رابط هذا التعليق
شارك

13 دقائق مضت, ابو جودي said:

ممكن عمل روتين عام ليتم استدعاءه من خلال optional
وذلك لارجاع اسم مربع النص الى الروتين كى لا تتم كتابة الاكواد كثيرا ان تعددت مربعات النص

نعم هذا ماانوي عمله ان شاء الله

بارك الله فيك استاذ محمد وجعل ما تقوم به من بذل للعلم والجهد والوقت في ميزان أعمالك

رابط هذا التعليق
شارك

اتفضل استاذى الجليل ومعلمى القدير و والدى الحبيب :fff:

 


Sub UpdateTableRecordsFromComboBox(targetComboBox As ComboBox, conditionField As String, updateField As String)
    Dim db As DAO.Database
    Dim strSQL As String
    Dim intconditionFieldValue As Integer
    Dim strupdateFieldValue As String
    Dim rsCombo As DAO.Recordset

    If targetComboBox Is Nothing Then MsgBox "Please select a valid ComboBox.": Exit Sub
    If Nz(conditionField, "") = "Please specify the condition field name." Then MsgBox "": Exit Sub
    If Nz(updateField, "") = "" Then MsgBox "Please specify the update field name.": Exit Sub

    ' Open the database
    Set db = CurrentDb

    ' Open a recordset for the values in targetComboBox
    Set rsCombo = db.OpenRecordset(targetComboBox.RowSource, dbOpenSnapshot)

    ' Disable the error handling temporarily
    On Error Resume Next

    ' Loop through each record in targetComboBox
    Do Until rsCombo.EOF
        ' Get the value from the current record in targetComboBox
        intconditionFieldValue = rsCombo.Fields(conditionField).Value
        strupdateFieldValue = rsCombo.Fields(updateField).Value

        ' Construct the SQL update statement
        strSQL = "UPDATE Table1 " & _
                 "SET text1 = '" & Nz(strupdateFieldValue, "") & "' " & _
                 "WHERE Table1.text1 = '" & intconditionFieldValue & "';"

        ' Execute the query
        db.Execute strSQL

        ' Check for errors
        If Err.Number <> 0 Then
            ' Clear the error
            Err.Clear
            ' Move to the next record without processing the error
            Resume Next
        End If

        ' Move to the next record in targetComboBox
        rsCombo.MoveNext
    Loop

    ' Enable the normal error handling
    On Error GoTo 0

    ' Close the recordset and the database
    rsCombo.Close
    Set rsCombo = Nothing
    Set db = Nothing
End Sub

وللاستدعاء

 

UpdateTableRecordsFromComboBox Me.Combo0, "id", "sName"

 

 

 

 

DatabaseUp3.rar

رابط هذا التعليق
شارك

استاذى الجليل ومعلمى القدير و والدى الحبيب الاستاذ @ابوخليل :fff:

فى المشاركة السابقة كان الروتين لابد من تمرير له
-مربع السرد مصدر البيانات
-اسم حقل الشرط
-اسم الحقل الذى يحوى القيم المراد التحديث مثلها
---------------------------------------------------------------------------------------

فى هذاالتعديل اضفت اسم الجدول واسم الحقل المراد تحديثه كذلك ليكون الروتين عام
بذلك لن نحتاج الى تعديل اى شئ فى بناء الكود مستقبلا مهما اختلفت الاسماء للجداول والحقول
بذلك يكون متاح لحضرتك كل الافكار فتدلل واختر منها ما يتماشى مع رغباتك ويلائم احتياجاتك وافكارك

تحديث جديد لتمرير اسم الجدول كذلك للروتين
على ان يكون الاستدعاء
 

UpdateTableRecordsFromComboBox "Table1", "text1", Me.Combo0, "id", "sName"



 

Sub UpdateTableRecordsFromComboBox(TableName As String, _
                                   FieldToUpdate As String, _
                                   TargetComboBox As ComboBox, _
                                   ConditionFieldName As String, _
                                   UpdateValueFieldName As String)
    
    
    Dim db As DAO.Database
    Dim strSQL As String
    Dim intConditionFieldValue As Integer
    Dim strUpdateFieldValue As String
    Dim rsCombo As DAO.Recordset


    ' Check if the tableName is specified
    If Nz(TableName, "") = "" Then MsgBox "Please specify the table name.", vbExclamation: Exit Sub
    
    ' Check if the FieldToUpdate is specified
    If Nz(FieldToUpdate, "") = "" Then MsgBox "Please specify the target field name to update.", vbExclamation: Exit Sub
    
    
    ' Check if the targetComboBox is valid
    If TargetComboBox Is Nothing Then MsgBox "Please select a valid ComboBox.": Exit Sub
    
    ' Check if the conditionField is specified
    If Nz(ConditionFieldName, "") = "Please specify the condition field name." Then MsgBox "": Exit Sub
    
    ' Check if the updateValueField is specified
    If Nz(UpdateValueFieldName, "") = "" Then MsgBox "Please specify the update field name.": Exit Sub
    

    ' Open the database
    Set db = CurrentDb

    ' Open a recordset for the values in targetComboBox
    Set rsCombo = db.OpenRecordset(TargetComboBox.RowSource, dbOpenSnapshot)

    ' Disable the error handling temporarily
    On Error Resume Next

    ' Loop through each record in targetComboBox
    Do Until rsCombo.EOF
        ' Get the value from the current record in targetComboBox
        intConditionFieldValue = rsCombo.Fields(ConditionFieldName).Value
        strUpdateFieldValue = rsCombo.Fields(UpdateValueFieldName).Value

        ' Construct the SQL update statement
        strSQL = "UPDATE " & TableName & " " & _
                 "SET " & FieldToUpdate & "= '" & Nz(strUpdateFieldValue, "") & "' " & _
                 "WHERE " & TableName & "." & FieldToUpdate & "= '" & intConditionFieldValue & "';"

        ' Execute the query
        db.Execute strSQL

        ' Check for errors
        If Err.Number <> 0 Then
            ' Clear the error
            Err.Clear
            ' Move to the next record without processing the error
            Resume Next
        End If

        ' Move to the next record in targetComboBox
        rsCombo.MoveNext
    Loop

    ' Enable the normal error handling
    On Error GoTo 0

    ' Close the recordset and the database
    rsCombo.Close
    Set rsCombo = Nothing
    Set db = Nothing
End Sub

 

 

DatabaseUp4.rar

  • Like 2
رابط هذا التعليق
شارك

أنا متابع للتعلم من لمساتك السوبر كبيرة أستاذي @ابو جودي والشكر والامتنان لأستاذنا @ابوخليل الذي أتاح لنا هذه المحاضرة المكثفة لننهل من علمكم .

شكراً جزيلاً وتقديري وامتناني لكم أساتذتي ومعلميني .:fff::clapping:

رابط هذا التعليق
شارك

انت طول الليل جالس شغال .. وانا تركتك ورحت انام .. فاعذرني حكم السن .. لا يمكنني مجاراة الشباب

ما شاء الله لا قوة الا بالله .. اختصرت علي الكثير جزاك الله خيرا

--------------------------------------------------------------------------------------------------

الآن دعني اخبرك الحكاية كما يقولون من طق طق الى ....

القيمة الرقمية في الجدول نأخذها من ذاكرة اكسس ... كيف ؟ اشرح .. هات زيادة في التفصيل

يوجد في المشروع روتين يسجل اي تغيير يحدث على الحقول داخل النماذج

الحقول النصية لا اشكال فيها

المشكلة فقط في مربعات التحرير فجميع قيم مربعات التحرير في المشروع رقمية

فالروتين قبل تحديث النموذج يأخذ قيمة مربعات التحرير السابقة وقيمتها الحالية

قيمته الحالية لا اشكال فيها حيث يمكنني التعامل مع العنصر الحالي واستخراج قيمة العمود الثاني النصية

ولكن المشكلة  في القيمة السابقة فهي عبارة عن قيمة فقط في ذاكرة أكس وليس لها اي ارتباط 

فأنا الآن اريد ترجمة الرقم الى قيمته النصية بناء على  (النموذج + مربع التحرير الخاص به)

هذه كل الحكاية :biggrin:

-----------------------------------------------------------------------------------------------------------------------

طيب نأتي للمفيد 

انت وصلت الى مرحلة متقدمة جدا  لا يمكن الوصول اليها بسهولة .. وبقي تكة .. لعلها لا تصعب عليك

انا عدلت على الجدول في المثال كما هو موجود عندي في المشروع .. حين تطلع عليه ستفهم المطلوب

المطلوب تحديث الحقل  بناء على اسم النموذج واسم الحقل الموجودات ضمن السجل

طبعا اضفت نموذج آخر ومربعات تحرير اخرى 

اعتقد المعيار في الروتين سيكون بحاجة الى اسم النموذج ، حيث ان الكود سيتم تشغيله من النموذج النشط

,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,

 

 

 

DatabaseUp5.rar

  • Like 2
رابط هذا التعليق
شارك

استاذى الجليل  ومعلمى القدير و والدلى الحبيب الاستاذ @ابوخليل 

اعتذر انا فى العمل الان لذلك اعتذر عن التأخير فى الرد

وهذا التعديل الاخير بناء على رغباتكم اتمنى ان شاء الله أن يكون ملائما ومناسبا

بصراحة مش قادر اسهل التعامل مع الدالة باستدعائها  باكتر من ذلك
اعرف ان كود الاستدعاء اصبح كبير جدا ويحتوى على العديد من المعاملات parameters 


Sub UpdateTableRecordsFromComboBox(TableName As String, _
                                   FieldToUpdate As String, _
                                   ConditionFieldNameTable As String, _
                                   ConditionComboBoxFieldNameTable As String, _
                                   frm As Form, _
                                   TargetComboBox As ComboBox, _
                                   ConditionFieldName As String, _
                                   UpdateValueFieldName As String)
    
    Dim db As DAO.Database
    Dim strSQL As String
    Dim varConditionFieldValue As Variant
    Dim varUpdateFieldValue As Variant
    Dim rsCombo As DAO.Recordset
    
    ' Check if the tableName is specified
    If Nz(TableName, "") = "" Then
        MsgBox "Please specify the table name.", vbExclamation
        Exit Sub
    End If
    
    ' Check if the FieldToUpdate is specified
    If Nz(FieldToUpdate, "") = "" Then
        MsgBox "Please specify the target field name to update.", vbExclamation
        Exit Sub
    End If
    
    ' Check if the targetComboBox is valid
    If TargetComboBox Is Nothing Then
        MsgBox "Please select a valid ComboBox.", vbExclamation
        Exit Sub
    End If
    
    ' Check if the conditionFieldTable is specified
    If Nz(ConditionFieldNameTable, "") = "" Then
        MsgBox "Please specify the condition field name for the table.", vbExclamation
        Exit Sub
    End If
    
    ' Check if the conditionField is specified
    If Nz(ConditionFieldName, "") = "" Then
        MsgBox "Please specify the condition field name.", vbExclamation
        Exit Sub
    End If
    
    ' Check if the updateValueField is specified
    If Nz(UpdateValueFieldName, "") = "" Then
        MsgBox "Please specify the update field name.", vbExclamation
        Exit Sub
    End If
    
    ' Check if the ConditionComboBoxFieldNameTable is specified
    If Nz(ConditionComboBoxFieldNameTable, "") = "" Then
        MsgBox "Please specify the condition ComboBox field name for the table.", vbExclamation
        Exit Sub
    End If
    
    ' Open the database
    Set db = CurrentDb
    
    ' Open a recordset for the values in targetComboBox
    Set rsCombo = db.OpenRecordset(TargetComboBox.RowSource, dbOpenSnapshot)
    
    ' Disable the error handling temporarily
    On Error Resume Next
    
    ' Loop through each record in targetComboBox
    Do Until rsCombo.EOF
        ' Get the value from the current record in targetComboBox
        varConditionFieldValue = rsCombo.Fields(ConditionFieldName).Value
        varUpdateFieldValue = rsCombo.Fields(UpdateValueFieldName).Value
    
        ' Construct the SQL update statement
        strSQL = "UPDATE " & TableName & " " & _
                 "SET " & FieldToUpdate & "='" & Nz(varUpdateFieldValue, "") & "' " & _
                 "WHERE " & TableName & "." & ConditionComboBoxFieldNameTable & "= '" & TargetComboBox.Name & "' AND " & _
                 TableName & "." & ConditionFieldNameTable & "= '" & Nz(frm.Name, "") & "' AND " & _
                 TableName & "." & FieldToUpdate & "= '" & varConditionFieldValue & "';"

    
        ' Debugging information
        ' Dbug.Print "SQL Statement: " & strSQL
    
        ' Execute the query
        db.Execute strSQL

        ' Check for errors
        If Err.Number <> 0 Then
            ' Clear the error
            Err.Clear
            ' Move to the next record without processing the error
            Resume Next
        End If
        
        ' Move to the next record in targetComboBox
        rsCombo.MoveNext
    Loop
    
    ' Enable the normal error handling
    On Error GoTo 0
    
    ' Close the recordset and the database
    rsCombo.Close
    Set rsCombo = Nothing
    Set db = Nothing
    strSQL = vbNullString
    varConditionFieldValue = vbNullString
    varUpdateFieldValue = vbNullString
End Sub


المرفق..
 

DatabaseUp6.rar

  • Like 2
رابط هذا التعليق
شارك

2 ساعات مضت, ابو جودي said:


وهذا التعديل الاخير بناء على رغباتكم اتمنى ان شاء الله أن يكون ملائما ومناسبا
 

ولا اروع ولا اجمل .. ويعمل بشكل صحيح ودقيق

وعلى طريقتك حبيبنا فيه حاجة صغننة .. سأحاول ان استطعت ان لم تسبقني

وهي تصور ان النموذج يشتمل على 10 مربعات او اكثر او اقل .. وهنا يلزمني  كتابة عشرة اسطر

فافكر اعمل متغير  ودوران يأخذ اسماء مربعات التحرير ... 

وفي الوقت نفسه يأخذ قيمة مربع التحرير وقيمة العمود الثاني من هذا السطر

Set rsCombo = db.OpenRecordset(targetComboBox.RowSource, dbOpenSnapshot)

اخي الغالي ابو جودي

انت في الف حل  فلا تشغل نفسك .. يكفيني النقطة التي اوصلتني اليها ، 

جزاؤك واجرك على الله 

  • Like 1
رابط هذا التعليق
شارك

  • أفضل إجابة
3 ساعات مضت, ابوخليل said:

انت في الف حل  فلا تشغل نفسك .. يكفيني النقطة التي اوصلتني اليها

ومن قال انه يكفينى انا ؟!
ومن قال اننى فى حل

اعتذر عن التأخير كنت صائم

 

  

3 ساعات مضت, ابوخليل said:

ولا اروع ولا اجمل


بل روعة حياتنا هم اساتذتنا العظماء الذين اناروا افكارنا
شكرا لكم استاذى الجليل و معلمى القدير و والدى الحبيب استاذ @ابوخليل :fff:سعادة  الحياة أن تكون بجانبك وتساندك روح طيبة وكريمة تمنحك وتهديك الفرح والسرور وتدخل على نفوس من حولها الفرح و السعادة
ولا أزكيكم على الله وكل اساتذتنا المبجلين كل كلمات ومعان الشكر والعرفان بالجميل لا تكفيكم وتوفيكم قدر حقكم 
بارك الله لكم فى عمركم وفى علمكم و عملكم واهلكم وأسال الله تعالى لكم سعادة الدارين وان يزيدكم من فضله كما تدخلون السرور على قلوب طلاب العلم
دائما تكثرون من العطاء وبكل سخاء دون كلل ولا ملل
احسن الله اليكم كما تحسنون الى طلاب العلم دائما 

-----
اتفضل استاذى الجليل ومعلمى القدير ووالدى الحبيب 
اولا الدالة الاتية لاحضار اسماء الحقول

 



Function GetFieldNameFromRowSource(ComboRowSource As String, columnIndex As Integer) As String
    Dim columns As Variant
    Dim columnName As String
    
    ' Split the RowSource to get the column names
    columns = Split(Mid(ComboRowSource, InStr(ComboRowSource, "SELECT") + Len("SELECT")), ",")
    
    ' Extract the column name from the specified index
    columnName = Trim(Split(Split(columns(columnIndex - 1), "AS")(0), ".")(1))
    
    ' Return the column name
    GetFieldNameFromRowSource = columnName
End Function

 وهذه الدالة التى يتم التحديث من خلالها


Sub GetComboBoxNameAndUpdateTableRecords(frm As Form)
    Dim ctrl As Control
    Dim ComboRowSource As String
    Dim FldData As String
    Dim FldID As String
    Dim strSQL As String
    
    ' Loop through all controls on the form
    For Each ctrl In frm.Controls
        ' Check if the control is a TextBox or a ComboBox
        If TypeOf ctrl Is comboBox Then
            ' Perform your custom action for each control
            ' For example, print the name and set a default value
            If Not ctrl Is Nothing Then
                ' Debug.Print "Control Name: " & ctrl.Name
                
                ComboRowSource = ctrl.rowSource
                FldID = GetFieldNameFromRowSource(ComboRowSource, 1)
                FldData = GetFieldNameFromRowSource(ComboRowSource, 2)
                
                
                Dim varConditionFieldValue As Variant
                Dim varUpdateFieldValue As Variant
                
                ' Open the database
                Dim db As DAO.Database
                Set db = CurrentDb
                
                ' Verify RowSource
                If Len(ComboRowSource) = 0 Then
                    ' Debug.Print "RowSource is empty for control " & ctrl.Name
                    Exit For
                End If
                
                ' Open a recordset for the values in targetComboBox
                Dim rsCombo As DAO.Recordset
                Set rsCombo = db.OpenRecordset(ComboRowSource, dbOpenSnapshot)
                
                ' Verify Recordset
                If rsCombo.EOF Then
                    ' Debug.Print "Recordset is empty for control " & ctrl.Name
                    rsCombo.Close
                    Set rsCombo = Nothing
                    Set db = Nothing
                    Exit For
                End If
                
                Do Until rsCombo.EOF
                
                    Dim TableName As String
                    Dim FieldToUpdate As String
                    Dim ConditionFieldNameTable As Variant
                    Dim ConditionComboBoxFieldNameTable As Variant
                    
                    ' Get the value from the current record in targetComboBox
                    varConditionFieldValue = rsCombo.Fields(FldID).Value
                    
                    varUpdateFieldValue = rsCombo.Fields(FldData).Value
                    
                    ' Table and Field Names
                    TableName = "Table1"
                    FieldToUpdate = "textNm"
                    ConditionFieldNameTable = "frmNm"
                    ConditionComboBoxFieldNameTable = "FieldNm"
                    
                    ' Construct the SQL update statement
                    strSQL = "UPDATE " & TableName & " " & _
                             "SET " & FieldToUpdate & "='" & Nz(varUpdateFieldValue, "") & "' " & _
                             "WHERE " & TableName & "." & ConditionComboBoxFieldNameTable & "= '" & ctrl.Name & "' AND " & _
                             TableName & "." & ConditionFieldNameTable & "= '" & Nz(frm.Name, "") & "' AND " & _
                             TableName & "." & FieldToUpdate & "= '" & varConditionFieldValue & "';"
                    
                    ' Debug.Print strSQL
                    ' Execute the query
                    db.Execute strSQL, dbFailOnError
                    
                    rsCombo.MoveNext
                Loop
                
                ' Close the recordset and the database
                rsCombo.Close
                Set rsCombo = Nothing
                Set db = Nothing
            End If
        End If
    Next ctrl
End Sub

ونستدعى الدالة فقط من خلال 

GetComboBoxNameAndUpdateTableRecords Me

هى سوف تقوم بكل شئ بالنيابة عنك :biggrin: 
لا تقلق منها يا معلملى هى دالة ذكية ليست مثلى



طبعا يا استاذى الجليل ومعلمى القدير و والدى الحبيب الاستاذ @ابوخليل 
ان اردت اضافة هذه المعاملات (  parameters  ) الى رأس الدالة لتكتبها عند الاستدعاء ان كانت متغيرة فلا بأس بذلك سبق التعامل معها فى الامثلة السابقة

' Table and Field Names
TableName = "Table1"
FieldToUpdate = "textNm"
ConditionFieldNameTable = "frmNm"
ConditionComboBoxFieldNameTable = "FieldNm"

 

بــــــس خلاص أخيرا خلصت الواجب الحمد لله :jump:
واخيرا المرفق 


 

DatabaseUp7.rar

  • Thanks 2
رابط هذا التعليق
شارك

ما شاء الله لا قوة الا بالله

هكذا العبقرية في الاحتراف والا فلا

سبحان الذي اعطاك الملكة والبديهة البرمجية

لا املك الا الدعاء لك .. بأن يحفظك الله في اهلك وولدك ومالك وصحتك ، وان يرزقك من حيث لا تحتسب

وأسأل الله الكريم ان يجعل ما تبذله من وقت وجهد .. عزا لك في الدنيا وراحة بال ، ورفعة لك في آخرتك

 

  • Thanks 1
رابط هذا التعليق
شارك

12 دقائق مضت, ابوخليل said:

ما شاء الله لا قوة الا بالله

هكذا العبقرية في الاحتراف والا فلا

سبحان الذي اعطاك الملكة والبديهة البرمجية

لا املك الا الدعاء لك .. بأن يحفظك الله في اهلك وولدك ومالك وصحتك ، وان يرزقك من حيث لا تحتسب

وأسأل الله الكريم ان يجعل ما تبذله من وقت وجهد .. عزا لك في الدنيا وراحة بال ، ورفعة لك في آخرتك

استاذى الجليل ومعلمى القدير و والدى الحبيب الاستاذ @ابوخليل :fff:

جزاكم الله خيــــرا على دعواتكم الطيبة اسأل الله تعالى ان يرزقكم فضلها واجرها اضعافا مضاعفة
لكم كل الفضل بعد رب العزة سبحانه وتعالى
انضممت اليكم ولا اعلم عن الاكسس الا اسمه فجزاكم الله خيـرا على رعايتكم بحب وتحملكم بحلم 
شكر الله لكم انتم وكل اساتذتى الذين اتعلم منهم 

واخيـــــر وليس آخرا احبكم فى الله  :wub:

  • Like 2
رابط هذا التعليق
شارك

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

زائر
اضف رد علي هذا الموضوع....

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

  • تصفح هذا الموضوع مؤخراً   0 اعضاء متواجدين الان

    • لايوجد اعضاء مسجلون يتصفحون هذه الصفحه
×
×
  • اضف...

Important Information