Îáðàáîòêà òàáëèö ñ èñïîëüçîâàíèåì ìàññèâîâ, â êîòîðûå ïåðåíîñÿòñÿ ýëåìåíòû òàáëèö (Ñïîñîá
Øàãè:
1.  ñåêöèè General Declaration ìîäóëÿ îáúÿâèòü äèíàìè÷åñêèå ãëîáàëüíûå äâóìåð-
íûå ìàññèâû, ñîîòâåòñòâóþùèå òàáëèöàì Postavka è Izd, è ãëîáàëüíûå ïåðåìåííûå:
Public P () As Variant
Public Iz () As Variant
Public I%, J%, KztP%, KztI%, L%, F%
2. Íà ôîðìå Form1 ðàñïîëîæèòü:
- ìàññèâ ýëåìåíòîâ TextBox(Text1(0) …Text1(5)) äëÿ ïîëåé çàïèñåé òàáëèöû Postavka (ñâÿçàííûå îáúåêòû);
- ýëåìåíò äàííûõ Data1;
- êíîïêè : Cmd1 – äëÿ çàïóñêà ïðîöåäóðû ïåðåäà÷è çíà÷åíèé ïîëåé çàïèñåé
òàáëèöû Postavka â ìàññèâ P è â ñåòêó Flp;
Cmd2 – äëÿ çàïóñêà ðåøåíèÿ çàäà÷è;
Ñmd3 – äëÿ ïåðåõîäà íà ôîðìó 2;
Cmd4 – äëÿ âûõîäà èç Ïðîåêòà;
- ìåòêó íàäïèñè ”Ñòîèìîñòü” íàä ýëåìåíòîì ListBox (ñ èìåíåì Lbl1);
- ìåòêó äëÿ óêàçàíèÿ èñêîìîãî âåñà èëè ñîîáùåíèÿ “Òàáëèöû íå ñîãëàñîâàíû” (ñ èìåíåì Lbl2);
- ýëåìåíò ListBox (ñ èìåíåì Lst1);
- ýëåìåíò MSFlexGrid (ñ èìåíåì Flp) äëÿ îòîáðàæåíèÿ òàáëèöû Postavka;
Ñâÿçàííûå ýëåìåíòû TextBox Ýëåìåíò óïðàâëåíèÿ Data1Lbl2(Óêàçàíèå èñêîìîãî âåñà
èëè ñîîáùåíèÿ – “Òàáëèöû íå
ñîãëàñîâàíû”)
Íàäïèñü íàä ýëåìåíòîì
ListBox (ýëåìåíò Lbl1)
ListBox (äëÿ âûâîäà îïðåäåëÿåìûõ
çàäàíèåì ñâåäåíèé
Êîìàíäíàÿ êíîïêà Cmd4
âûõîäà èç Ïðîåêòà
Êîìàíäíàÿ êíîïêà Cmd3
ïåðåõîäà íà ôîðìó Form2
Êîìàíäíàÿ êíîïêà Cmd1
çàïóñêà ïðîãðàììû ïåðåäà÷è çíà÷åíèé ïîëåé
òàáëèöû Postavka â ìàññèâ P è â
MSFlexGrid – Ãèáêàÿ ñåòêà ñ èìåíåì Flp ñåòêó Flp
Êîìàíäíàÿ êíîïêà Cmd2
çàïóñêà ðåøåíèÿ çàäà÷è
Ðèñ. 6.3. Ýêðàííàÿ ôîðìà äëÿ ðàáîòû ñ ÁÄ ñ òàáëèöåé (Postavka)
3. Óñòàíîâèòü ñâîéñòâà îáúåêòà Data1:
Name = Data1
Caption = Tàáëèöà Postavka
Connect = Access
DataBaseName = “H:\ . . . . .” – ïîëíûé ïóòü ê ôàéëó
RecordSource = Postavka – èìÿ òàáëèöû – èñòî÷íèê çàïèñåé
RecordSetType = 0
4. Óñòàíîâèòü ñâîéñòâà ñâÿçàííûõ ýëåìåíòîâ :
Name = Text1(0)
DataSource = Data1 - èñòî÷íèê çàïèñåé
DataField = ShIzd - ïîëå òàáëèöû
Name = Text1(1)
DataSource = Data1
DataField = NameIzd
. . .
Name = Text1(5)
DataSource = Data1
DataField = Pkv4
5. Óñòàíîâèòü îñíîâíûå ñâîéñòâà ñåòêè MSFlexGrid, ýëåìåíòîâ ListBox è Label :
Name = Flp; Name
= List1; Name = Lbl1; Name = Lbl2.
- ýëåìåíò äàííûõ Data2;
6. Ñîçäàòü Form2. Íà íåé ðàñïîëîæèòü:
- ìàññèâ ýëåìåíòîâ TextBox (Text2(0) …Text2(3)) äëÿ ïîëåé çàïèñåé
òàáëèöû Izd (ñâÿçàííûå îáúåêòû);
Ñâÿçàííûå ýëåìåíòû
TextBox
Ýëåìåíò óïðàâëåíèÿ Data2
Êîìàíäíàÿ êíîïêà Cmd5 äëÿ
çàïóñêà ïðîöåäóðû ïåðåäà÷è
çíà÷åíèé ïîëåé çàïèñåé òàáëè-
öû Izd â ìàññèâ Iz è â ñåòêó Fli
Êîìàíäíàÿ êíîïêà Cmd6 äëÿ
ïåðåõîäà íà ôîðìó Frm1
MSFlexGrid – ãèáêàÿ ñåòêà
ñ èìåíåì FLi
Ðèñ. 6.4. Ýêðàííàÿ ôîðìà äëÿ ðàáîòû ñ ÁÄ (ñ òàáëèöåé Izd)
- êíîïêè:
Cmd5 – äëÿ çàïóñêà ïðîöåäóðû ïåðåäà÷è çíà÷åíèé ïîëåé çàïèñåé
òàáëèöû Izd â ìàññèâ Iz è â ñåòêó Fli;
Ñmd6 – äëÿ ïåðåõîäà íà ôîðìó Form1;
- ýëåìåíò MSFlexGrid (ñ èìåíåì Fli) äëÿ îòîáðàæåíèÿ íóæíûõ ïîëåé
- òàáëèöû Izd.
7. Óñòàíîâèòü ñâîéñòâà îáúåêòà Data2:
Name = Data2
Caption = Tàáëèöà Izd
Connect = Access
DataBaseName = “H:\ . . . . .” – ïîëíûé ïóòü ê ôàéëó
RecordSource = Izd – èìÿ òàáëèöû – èñòî÷íèê çàïèñåé
RecordSetType = 0
8. Óñòàíîâèòü ñâîéñòâà ñâÿçàííûõ ýëåìåíòîâ:
Name = Text2(0)
DataSource = Data2 - èñòî÷íèê çàïèñåé
DataField = ShIzd - ïîëå òàáëèöû
Name = Text2(1)
DataSource = Data2
DataField = NameIzd
Name = Text2(2)
DataSource = Data2
DataField = Cena
Name = Text2(3)
DataSource = Data2
DataField = Ves
9. Óñòàíîâèòü ñâîéñòâà ñåòêè MSFlexGrid :
Name = Fli
10. Ðàçðàáîòàòü ïðîöåäóðû Form1:
Private Sub Cmd1_Click ( ) ‘ïðîöåäóðà (1), ñ÷èòûâàíèå òàáëèöû Postavka â _
ìàññèâ P è â ñåòêó Flp
Data1.RecordSet.MoveFirst ‘ óñòàíîâêà íà ïåðâóþ çàïèñü òàáëèöû Postavka
KztP = Data1.RecordSet.RecordCount ‘êîëè÷åñòâî çàïèñåé òàáëèöû Postavka
KptP = Data.RecordSet.Fields.Count ‘êîëè÷åñòâî ïîëåé òàáëèöû Postavka
ReDim P(1 To KztP, 1 To KptP)
Flp.Rows = KztP+1 : Flp.Cols = KptP ‘óñòàíîâëåíèå ðàçìåðîâ ñåòêè Flp
For I = 1 To KztP
For J = 1 To KptP
If I = 1 Then Flp.TextMatrix(I-1, J-1) = Data1.RecordSet.Fields(J-1).Name
P(I, J) = Text1(J-1) : Flp.TextMatrix(I, J-1) = P(I, J) ‘èëè = Text1(J-1), _
èëè Data1.RecordSet.Fields(J-1).Value
Next J
Data1.RecordSet.MoveNext ‘ïåðåõîä ê ñëåäóþùåé çàïèñè
Next I
End Sub
Private Sub Cmd2_Click ( ) ‘ïðîöåäóðà (2), ñîâìåñòíàÿ îáðàáîòêà òàáëèö ÁÄ
Dim St(1 To 4) As Single ‘ìàññèâ ñòîèìîñòè ïîñòàâëÿåìûõ èçäåëèé ïî êâàðòàëàì
Dim Sh As String
, V! ‘Sh – øèôð (èçäåëèÿ), V–îáùèé âåñ èçäåëèé èñêîìîãî øèôðà
KztI = Data2.RecordSet.RecordCount ‘êîëè÷åñòâî çàïèñåé òàáëèöû Izd
V = 0
For L = 1 To 4 ‘îáíóëåíèå ìàññèâà St
St(L) = 0
Next L
For I = 1 To KtzP ‘ïî ñòðîêàì òàáëèöû Postavka
Sh = P(I, 1) ‘Â Sh çàíîñèòñÿ øèôð èçäåëèÿ òåêóùåé ñòðîêè òàáëèöû Postavka
F = 0 ‘ôëàæîê
For J = 1 To KztI ‘ïî ñòðîêàì òàáëèöû Izd
If Iz (J, 1) = Sh Then ‘ðàâåíñòâî øèôðîâ â òàáëèöàõ Postavka è Izd
For L = 1 To 4
St(L) = St(L) + P(I, L+2) * Iz(J,3) ‘âû÷èñëåíèå ñòîèìîñòè ïîñòàâ- _
ëÿåìîãî èñêîìîãî èçäåëèÿ ïî êâàðòàëàì
V = V + P(I, L+2) * Iz(J, 4) ‘âû÷èñëåíèå îáùåãî âåñà èçäåëèÿ _
èñêîìîãî øèôðà
Next L
F=1 : Exit For ‘Òàáëèöû ñîãëàñîâàíû. Äàííûå ïîëó÷åíû. Ôëàæîê
èçìåíåí âûõîä èç öèêëà J
End If
Next J
If F = 0 Then Lbl2.Caption = “Òàáëèöû íå ñîãëàñîâàíû” : Exit Sub
Next I
Lbl2.Caption = “Èñêîìûé âåñ=” & Str(V)
For L=1 To 4
Lst1.List (L-1) = St(L)
Next L
End Sub
Private Sub Form1.Cmd3_Click ( ) ‘ ïðîöåäóðà (3)
Form1.Hide ‘ñïðÿòàòü ôîðìó Form1
Form2.Show ‘ïîêàçàòü ôîðìó
Form2
End Sub
Private Sub Form1.Cmd4_Click ( ) ‘ïðîöåäóðà (4), âûõîä èç Ïðîåêòà
End
End Sub
Private Sub Cmd5_Click
( ) ‘ïðîöåäóðà (5), ñ÷èòûâàíèå òàáëèöû Izd â ìàññèâ Iz _
è â ñåòêó Fli
Data2.RecordSet.MoveFirst ‘óñòàíîâêà íà ïåðâóþ çàïèñü òàáëèöû Izd
KztI = Data2.RecordSet.RecordCount ‘êîëè÷åñòâî çàïèñåé òàáëèöû Izd
KptI = Data.RecordSet.Fields.Count ‘êîëè÷åñòâî ïîëåé òàáëèöû Izd
ReDim Iz(1 To KztI, 1 To KptI)
Fli.Rows = KztI +1 : Fli.Cols = KptI ‘óñòàíîâëåíèå ðàçìåðîâ ñåòêè Fli
For I = 1 To KztI ‘ïî ñòðîêàì òàáëèöû Izd
For J = 1 To KptI
If I = 1 Then Fli.TextMatrix (I-1, J-1) = Data2.RecordSet.Fields(J-1).Name
Iz(I, J) = Text2(J-1) : Fli.TextMatrix(I, J-1) = Iz(I, J) ‘èëè = Text2(J-1), _
èëè Data2.RecordSet.Fields(J-1).Value
Next J
Data2.RecordSet.MoveNext ‘ïåðåõîä ê ñëåäóþùåé çàïèñè
Next I
End Sub
Private Sub Cmd6_Click ( )
Form2.Hide ‘ñïðÿòàòü ôîðìó Form2
Form1.Show ‘ïîêàçàòü ôîðìó
Form1
End Sub
Äåéñòâèÿ:
- çàïóñòèòü Ïðîåêò;
- íà Form1 íàæàòü Cmd1 ( âûïîëíÿåòñÿ ïðîöåäóðà ñ÷èòûâàíèÿ òàáëèöû Postavka â
ìàññèâ P
è â ñåòêó Flp), çàòåì Cmd3 (îñóùåñòâëÿåòñÿ ïåðåõîä íà ôîðìó Form2);
- íà Form2 íàæàòü Cmd5 (âûïîëíÿåòñÿ ïðîöåäóðà ñ÷èòûâàíèÿ òàáëèöû Izd â ìàññèâ Iz è â ñåòêó Fli), çàòåì Cmd6 (îñóùåñòâëÿåòñÿ ïåðåõîä íà ôîðìó Form1);
- íà Form1 íàæàòü Cmd2 (ðåøåíèå çàäà÷è);
- íà Form1 íàæàòü Cmd4 (âûõîä èç Ïðîåêòà).
-