שאלה באקסס+ WORD

הגנובה1

New member
שאלה באקסס+ WORD

בניתי userform ב-WORD עם 2 combobox: 1. country 2. city יצרתי באקסס טבלה הנקראת tblAll - ובה שני הטורים הללו בלבד. כתבתי את הקוד הבא:
Option Explicit Private Sub cboCountry_change() Dim dbDatabase As Database Dim rst As Recordset Dim y As Integer Dim strSQL As String Set dbDatabase = OpenDatabase("C:\phone.mdb") Set rst = dbDatabase.OpenRecordset("SELECT DISTINCT city FROM tblAll WHERE Country = '" & cboCountry.Text & "' ORDER BY city;", dbOpenSnapshot) y = 0 'This code populates the combo box with the values in the CompanyName field. With rst On Error Resume Next 'strSQL = "Select city " & _ ' "FROM tblAll " & _ ' "WHERE Country = '" & cboCountry.Text & "' " & _ ' "ORDER BY city;" Do Until .EOF cboCity.AddItem (y) cboCity.ColumnCount = 3 cboCity.BoundColumn = 3 cboCity.ColumnWidths = "6 in" cboCity.AutoTab = True cboCity.Column(0, y) = rst.Fields("city") .MoveNext y = y + 1 Loop rst.Close 'db.Close Set rst = Nothing 'Set db = Nothing End With 'MsgBox cboCountry.Text 'cboCity.Value = strSQL End Sub Private Sub UserForm_Initialize() Dim dbDatabase As Database Dim rs As Recordset Dim i As Integer ' This code activates the Database connection. (Change the path to reflect your database). Set dbDatabase = OpenDatabase("C:\phone.mdb") ' This code opens the Managers table. (Change the Table to reflect the desired table). Set rs = dbDatabase.OpenRecordset("SELECT DISTINCT Country FROM tblAll ORDER BY Country;", dbOpenSnapshot) i = 0 'This code populates the combo box with the values in the CompanyName field. With rs On Error Resume Next Do Until .EOF cboCountry.AddItem (i) cboCountry.ColumnCount = 3 cboCountry.BoundColumn = 3 cboCountry.ColumnWidths = "6 in" cboCountry.AutoTab = True cboCountry.Column(0, i) = rs.Fields("country") .MoveNext i = i + 1 Loop End With End Sub​
הקוד עובד ועושה בדיוק את מה שאני רוצה.... בקומבו הראשון מופיעה רשימת הארצות ובקומבו השני מופיעות הערים הרלוונטיות... הבעיה הגדולה היא שאם בחרתי ארץ ועיר ואח"כ התחרטתי (בלי לצאת מהטופס) ובחרתי ארץ ועיר אחרות... אני מקבלת בקומבו השני (של הערים) עוד רשימת מספרים (נוסף על הערים הרלונטיות)... כל פעם נוספים אותם מספרים: 0 1 0 1 0 1 או 0 1 2 0 1 2 וכו'... אשמח עם מישהו יוכל להגיד לי היכן טעיתי... אולי אני צריכה לסגור את הDB או את הרשומה באיזה מקום ולא עושה את זה נכון? אני כבר לא יודעת מה לחשוב.... תודה מראש
 
למעלה