Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Grid Focus?

Status
Not open for further replies.

savok

Technical User
Jan 11, 2001
303
AT
I have a grid, and when you click and select something on it a command button is enabled.

In form load the grid is loaded with data. For some reason after its loaded it executes the on click code and the command button is enabled. Why does it do that? when the grid wasnt actually clicked on? And what can I do to stop the command button from being enabled unless the grid is actually clicked on.

thank you.
 
It depends on the grid but you could do something like this

Option Explicit
Dim bLoaded As Boolean

Private Sub Form_Load()
'code
'code
bLoaded = True
End Sub

Private Sub Grid_Click()
If bLoaded Then
Command1.Enabled = True
End If
End Sub

Hope this helps. If you choose to battle wits with the witless be prepared to lose.
[machinegun][hammer]
 
Its a Flex Grid.

I actually need the Command1.Enabled = False unless the grid was clicked on with a mouse.

thanks

here is the code...

Private Sub Grid_Click()
'Get selected rec
cmdDelete.Enabled = True
End Sub

When the grid is clicked on, cmdDelete is enabled so I can delete the selected record. But after the grid is loaded with data it executes the click event and selects the first loaded record for some reason, thus enabling the Delete cmd.
When it should be disabled unless the user actually clicks on the grid.

thank you.
 
At the end of the form load routine, after the grid is loaded add the following statement:

cmdDelete.Enabled = False
Good Luck
--------------
As a circle of light increases so does the circumference of darkness around it. - Albert Einstein
 
Maybe I didn't understand what you are getting at, but the "bLoaded" variable will be false until after the form is loaded therefore the Grid_Click() event will not do anything until after the form is done loading. If you set the command button to enabled = false as a default then the click event above will only enable the command button after the form is loaded and the grid is clicked. Is that what you are looking to do? If you choose to battle wits with the witless be prepared to lose.
[machinegun][hammer]
 
yes but the thing is the grid was never actually clicked on by the user. It executes the grid_click all by it self without being clicked on, and i dont know why.

Private Sub Form_Load()
cmdDelete.Enabled = False
Load_Grid
bLoaded = True
End Sub

Private Sub Grid_Click()
intGridID = Grid.TextMatrix(Grid.RowSel, 0)
MsgBox ("here")
If bLoaded Then
cmdDelete.Enabled = True
End If
End Sub

here i did what you suggested and it still does the same thing.

thanks
 
Private Sub Form_Load()
Load_Grid
cmdDelete.Enabled = False
End Sub

Private Sub Grid_Click()
intGridID = Grid.TextMatrix(Grid.RowSel, 0)
MsgBox ("here")
cmdDelete.Enabled = True
End Sub
If you choose to battle wits with the witless be prepared to lose.
[machinegun][hammer]
 
thats exactly what i had before you suggested using the bLoaded stuff...doesnt work ;(
 
What foada has suggested should work fine. I am assuming that during the form load, you are getting the msgbox "here" from the grid_click event. If that is so, then there is either something in the Load_Grid that is causing the click event to fire, or there is something else in the Form Load that we're not seeing, or something from another routine that is being called in the Form load procedure. Without seeing all of the code in the Form Load, and code referenced therein, it will be difficult to see what triggers the click event.

Good Luck
--------------
As a circle of light increases so does the circumference of darkness around it. - Albert Einstein
 
CajunCenturion, I already have set to False at the end of the Load Grid routine. It executes the grid_click after, but i dont know why.
 
k here is the code...

Private Sub Form_Load()
MDIMain.Center Me
MDIMain.DisableCloseWindowButton Me
Call Load_cmbEvent
Call Load_cmbFacilitator
Call Load_cmbLocation
Call cmbLocation_Click
If intScheduleEdit = 2 Then
Call Load_Boxes
Call Load_grdRoster
End If
If intScheduleEdit = 1 Then
cmdAdd.Enabled = False
cmdExisting.Enabled = False
End If
cmdDelete.Enabled = False
End Sub

Private Sub Load_grdRoster()
Dim strSSN As String
Dim strLN As String
Dim strFN As String
Set DBComm = New ADODB.Command
Set DBRec = New ADODB.Recordset
DBComm.ActiveConnection = DBConn
DBComm.CommandText = "Select * Tables"
DBComm.CommandType = adCmdText
Set DBRec.Source = DBComm
DBRec.Open
If DBRec.EOF Or DBRec.BOF Then
grdRoster.Enabled = False
grdRoster.Clear
grdRoster.Cols = 0
grdRoster.Rows = 0

Else
grdRoster.Clear
grdRoster.Enabled = True
grdRoster.Cols = 5
grdRoster.Rows = 2
grdRoster.FixedRows = 1

grdRoster.Col = 0
grdRoster.Row = 0
grdRoster.ColWidth(0) = 0

grdRoster.Col = 1
grdRoster.Row = 0
grdRoster.ColWidth(1) = 500
grdRoster.ColAlignment(1) = 4
grdRoster.Text = "#"

grdRoster.Col = 2
grdRoster.Row = 0
grdRoster.ColWidth(2) = 1000
grdRoster.ColAlignment(2) = 4
grdRoster.Text = "SSN"

grdRoster.Col = 3
grdRoster.Row = 0
grdRoster.ColWidth(3) = 4357
grdRoster.ColAlignment(3) = 1
grdRoster.Text = "NAME"

grdRoster.Col = 4
grdRoster.Row = 0
grdRoster.ColWidth(4) = 1500
grdRoster.ColAlignment(4) = 4
grdRoster.Text = "ATTENDANCE"

If DBRec.RecordCount <> 0 Then
i = 1
While Not DBRec.EOF
If i + 1 = grdRoster.Rows Then
grdRoster.Rows = grdRoster.Rows + 1
End If
If Not IsNull(DBRec(&quot;RosterID&quot;)) Then
grdRoster.TextMatrix(i, 0) = DBRec(&quot;RosterID&quot;)
End If
grdRoster.TextMatrix(i, 1) = i
If Not IsNull(DBRec(&quot;SSN&quot;)) Then
strSSN = DBRec(&quot;SSN&quot;)
If strSSN = &quot;&quot; Then
Else
strSSN = Mid(strSSN, 1, 3) & &quot;-&quot; & Mid(strSSN, 4, 2) & &quot;-&quot; & Mid(strSSN, 6, 4)
End If
grdRoster.TextMatrix(i, 2) = strSSN
End If
If Not IsNull(DBRec(&quot;LastName&quot;)) Then
strLN = DBRec(&quot;LastName&quot;)
End If
If Not IsNull(DBRec(&quot;FirstName&quot;)) Then
strFN = DBRec(&quot;FirstName&quot;)
End If
grdRoster.TextMatrix(i, 3) = strLN + &quot;, &quot; + strFN
If Not IsNull(DBRec(&quot;Attendance&quot;)) Then
grdRoster.TextMatrix(i, 4) = DBRec(&quot;Attendance&quot;)
End If
i = i + 1
DBRec.MoveNext
Wend
grdRoster.Rows = grdRoster.Rows - 1
End If
End If
DBRec.Close
'//////////Lock Add if Seat Limit is reached
Dim Limit As Integer
Limit = i - 1
If intSeatLimitPass <= Limit Then
cmdAdd.Enabled = False
cmdExisting.Enabled = False
cmdDelete.Enabled = False
txtSeatLimit.BackColor = &HFF&
Else
txtSeatLimit.BackColor = &H80000005
cmdDelete.Enabled = False
End If
cmdDelete.Enabled = False
'//////////
End Sub
 
OK, go easy. There is 2 suggestions going on at once. Is there any other code involved with the form, command button or the grid? Any other events? If you choose to battle wits with the witless be prepared to lose.
[machinegun][hammer]
 
Private Sub grdRoster_Click()
intRosterID = grdRoster.TextMatrix(grdRoster.RowSel, 0)
cmdDelete.Enabled = True
End Sub

Private Sub Load_Boxes()
Dim strYN As String
Dim intYN As Integer
Dim intRoom As Integer
Dim intLocation As Integer
Dim intFacilitator As Integer
Dim intEvent As Integer



Set DBComm = New ADODB.Command
Set DBRec = New ADODB.Recordset
DBComm.ActiveConnection = DBConn
DBComm.CommandText = &quot;Select * from Schedule where ScheduleID = &quot; & intScheduleID & &quot; &quot;
DBComm.CommandType = adCmdText
Set DBRec.Source = DBComm
DBRec.Open
If DBRec.EOF Or DBRec.BOF Then
MsgBox &quot;No records found.&quot;, vbInformation + Only
Else
If Not IsNull(DBRec(&quot;RoomID&quot;)) Then
intRoom = DBRec(&quot;RoomID&quot;)
End If
If Not IsNull(DBRec(&quot;LocationID&quot;)) Then
intLocation = DBRec(&quot;LocationID&quot;)
End If
If Not IsNull(DBRec(&quot;FacilitatorID&quot;)) Then
intFacilitator = DBRec(&quot;FacilitatorID&quot;)
End If
If Not IsNull(DBRec(&quot;EventID&quot;)) Then
intEvent = DBRec(&quot;EventID&quot;)
End If
If Not IsNull(DBRec(&quot;StartDate&quot;)) Then
txtStartDate = Format(DBRec(&quot;StartDate&quot;), &quot;mm/dd/yyyy&quot;)
End If
If Not IsNull(DBRec(&quot;EndDate&quot;)) Then
txtEndDate = Format(DBRec(&quot;EndDate&quot;), &quot;mm/dd/yyyy&quot;)
End If
If Not IsNull(DBRec(&quot;StartTime&quot;)) Then
txtStartTime = Format(DBRec(&quot;StartTime&quot;), &quot;hh:mm AMPM&quot;)
End If
If Not IsNull(DBRec(&quot;EndTime&quot;)) Then
txtEndTime = Format(DBRec(&quot;EndTime&quot;), &quot;hh:mm AMPM&quot;)
End If
If Not IsNull(DBRec(&quot;SeatLimit&quot;)) Then
txtSeatLimit = DBRec(&quot;SeatLimit&quot;)
intSeatLimitPass = DBRec(&quot;SeatLimit&quot;)
End If
If Not IsNull(DBRec(&quot;Comments&quot;)) Then
txtComments = DBRec(&quot;Comments&quot;)
End If
For idx = 0 To cmbEvent.ListCount - 1
If cmbEvent.ItemData(idx) = intEvent Then
cmbEvent.ListIndex = idx
Exit For
End If
Next idx
For idx = 0 To cmbFacilitator.ListCount - 1
If cmbFacilitator.ItemData(idx) = intFacilitator Then
cmbFacilitator.ListIndex = idx
Exit For
End If
Next idx
For idx = 0 To cmbLocation.ListCount - 1
If cmbLocation.ItemData(idx) = intLocation Then
cmbLocation.ListIndex = idx
Exit For
End If
Next idx
For idx = 0 To cmbRoom.ListCount - 1
If cmbRoom.ItemData(idx) = intRoom Then
cmbRoom.ListIndex = idx
Exit For
End If
Next idx
End If
'DBRec.Close
End Sub
 
Also to get to this form i have to either double click on a grid on another form or click the button. Now when i click the button i dont have this happening, but when i double click the grid this happens. But the code in the double click of the grid and the command button is identical on that other form.
 
In order to try and determine what is causing the click event to fire, I would comment out every call line in the form load, then one at a time, restore a call, leaving the grdload as the last call to reinstate. Lets insure that the event is being fired from something inside the grid load. You're calling quite a few Load routines, and a location_click event. The code in any of those routines may start a sequence of events which cause the grid click event to fire.

Are there any other grid event handlers active? Good Luck
--------------
As a circle of light increases so does the circumference of darkness around it. - Albert Einstein
 
Just did what you suggested, its def the grdLoad
 
Also if I add a msgbox(&quot;test&quot;) at the end of the form_load the problem disappears, as soon as i remove msgbox it starts executing the grd_click again
 
In your post: Also to get to this form i have to either double click on a grid on another form or click the button. Now when i click the button i dont have this happening, but when i double click the grid this happens. But the code in the double click of the grid and the command button is identical on that other form.

The fact that when you access this form thru the button click does not lead to the grid click event being fired, suggests that its highly unlikely that the cause is in the code of this form. If the cause were something in the Form Load, or the Load_grdRoster of this form, then it shouldn't matter how you activate the form, you should always encounter the event.

Lets focus then on the double click, which is on a grid of the parent form. I have had difficulties with the DblClick event of a grid, espcially a single click event is also enabled for that grid. Is there a single click event on the parent form's grid? If so, what does it do? Good Luck
--------------
As a circle of light increases so does the circumference of darkness around it. - Albert Einstein
 
thanks a lot for your help :)

heres the code of the parent form

Private Sub grdSchedule_Click()
intScheduleID = grdSchedule.TextMatrix(grdSchedule.RowSel, 0)
cmdEdit.Enabled = True
cmdDelete.Enabled = True
Enable_Reports (True)
cmdView.Enabled = True
End Sub

Private Sub grdSchedule_DblClick()
Call cmdEdit_Click
End Sub

Private Sub cmdEdit_Click()
frmSchedule.Hide
intScheduleEdit = 2 'Edit
frmScheduleEdit.Show
End Sub

the problem doesnt happen when you get to the form by clicking on the cmdEdit, only when you double click.
 
As I said earlier, I have have never been able to satisfactorily been able to combine single click and double click events on the same grid with any degree of consistency of behavior. My suggestion would be to elminate the DblClick event, and only allow the cmdEdit button to activate the ScheduleEdit form. Good Luck
--------------
As a circle of light increases so does the circumference of darkness around it. - Albert Einstein
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top