اذهب الي المحتوي
أوفيسنا

سؤال في الحلقة التكرارية


النافذ

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

بسم الله الرحمن الرحيم

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

فلقد سبق وأن سألت عن الحلقة التكرارية في مشاركة سابقة وكان السؤال :

كيف لي أن أغير مثلاً حقل درجة الطالب في جدول بإضافة درجة واحدة لكل طالب باستخدام الكود وليس الاستعلام، فأنا أعرف طريقة الاستعلام.

ما أريده هو كيف لي أن أستخدم حلقة تكرارية تمر على أول سجل في حقل ما إلى آخر سجل في الجدول ..

باستخدام For..Next أو For .. Each .. Next

لاني حاولت الوصول إلى Syntax الصحيح ولم أتمكن من ذلك .. فهل من إجابة ؟

و وصل رداً من الأخ مهند وكان :

يمكنك فعل ذلك .. بواسطة حلقة Do .. Loop

هذا مثال :

Dim D as Object<!--QuoteEBegin-->Set D = CurrentDb.OpenRecordset("Students")<!--QuoteEBegin-->    With D<!--QuoteEBegin-->           Do while not(.EOF)<!--QuoteEBegin-->               .Edit<!--QuoteEBegin-->               .StudentDegree = .StudentDegree+1<!--QuoteEBegin-->               .Update<!--QuoteEBegin-->               .MoveNext<!--QuoteEBegin-->           Loop<!--QuoteEBegin-->    End With<!--QuoteEBegin-->    Set D = Nothing

وهذا هو رابط المشاركة :

http://www.officena.net/ib/index.php?showt...205&hl=do+while

سؤالي :

كيف لي أن أجعل هذا التأثير في الإضافة على أكثر من حقل ، بمعنى ولتكن لدي عدد مرن من الحقول وأريد إضافة 1 لكل حقل .. ما أقصده كيف لي أن أنفذ حلقة تكرارية أخرى بالعرض على الحقول نفسها صفاُ داخل الحلقة السابقة ..

ولقد حاولت باستخدام الحلقة

For .. Each .. Next

لكن لم استطع الوصول إلى الحل ...

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

وشكرا . :fff:

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

Dim D as Object
Set D = CurrentDb.OpenRecordset("Students")    
With D           
Do while not(.EOF)
    .Edit               
    For R = 1 to 5
         .field(r) = .field(r)+1
    next
   .Update  
   .MoveNext           
Loop    
End With    
Set D = Nothing

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

أخي الكريم الحل أن تضع أسماء الحقول التي تريد الزيادة في مصفوفة .. كما في هذا الكود :

Dim D as Object , FN(5) as string
FN(0) = "FieldName1"
FN(1) = "FieldName2"
.
.
.
.
Set D = CurrentDb.OpenRecordset("Students")    
With D           
Do while not(.EOF)
   .Edit               
   For R = 1 to 5
        .field(FN(r)) = .field(FN(r))+1
   next
  .Update  
  .MoveNext           
Loop    
End With    
Set D = Nothing

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

أخي مهند .. حتى الآن لم أصل إلى ما أريد ..

لأبين لك ..

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

المشكلة أن هذه الجداول مختلفة في مسميات الحقول وعددها، فمرة يكون عددها 100 حقل ومرات 150 ومرات 50 ، والاسماء تختلف.

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

فأريد حل لايعتمد على اسماء الحقول وعددها، إنما حلقة تكرارية تعمل من اول حقل وحتى نهاية حقل أيا كان العدد. EOF

وجد كوداً مشابهاً لما أريد في الـ Help الأكسس لكن اختلفت طريقة الاتصال بقاعدة البيانات وبالتالي هناك فرق في كتابة الكود..

Dim cnn1 As ADODB.Connection
    Dim rstEmployees As ADODB.Recordset
    Dim fldLoop As ADODB.Field
    Dim proLoop As ADODB.Property
    Dim strCnn As String
    
    ' Open connection and recordset.
    strCnn = "Provider=sqloledb;" & _
        "Data Source=srv;Initial Catalog=Pubs;User Id=sa;Password=; "
    Set cnn1 = New ADODB.Connection
    cnn1.Open strCnn
    Set rstEmployees = New ADODB.Recordset
    rstEmployees.Open "employee", cnn1, , , adCmdTable
    
    ' Display the attributes of the connection.
    Debug.Print "Connection attributes = " & _
        cnn1.Attributes

    ' Display the attributes of the Employee table's
    ' fields.
    Debug.Print "Field attributes:"
    For Each fldLoop In rstEmployees.Fields
        Debug.Print "    " & fldLoop.Name & " = " & _
            fldLoop.Attributes
    Next fldLoop

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

Dim D as Object
Set D = CurrentDb.OpenRecordset("Students")    
With D           
Do while not(.EOF)
   .Edit               
   For R = 0 to D.Fields.Count-1
        .field(r) = .field(r)+1
   next
  .Update  
  .MoveNext           
Loop    
End With    
Set D = Nothing

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

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