Click here to Skip to main content
15,922,696 members
Home / Discussions / Visual Basic
   

Visual Basic

 
QuestionDatagrid: how to detect uncommitted changes? Pin
Roger Harvest4-May-06 6:39
Roger Harvest4-May-06 6:39 
QuestionPInvoke stack balance detected? Pin
Rashar4-May-06 6:25
Rashar4-May-06 6:25 
AnswerRe: PInvoke stack balance detected? Pin
kasik4-May-06 7:23
kasik4-May-06 7:23 
QuestionVB.Net Arrays Pin
passionirie4-May-06 5:22
passionirie4-May-06 5:22 
AnswerRe: VB.Net Arrays Pin
J4amieC4-May-06 5:40
J4amieC4-May-06 5:40 
QuestionCOMBOBOX Pin
moveman4-May-06 5:20
moveman4-May-06 5:20 
AnswerRe: COMBOBOX Pin
Quecumber2564-May-06 6:01
Quecumber2564-May-06 6:01 
AnswerRe: COMBOBOX Pin
Quecumber2564-May-06 6:28
Quecumber2564-May-06 6:28 
Moveman,
Here is the code I have. I'll try to explain the method to my madness. BTW, you can use this code for both combo and list boxes.

Step 1 - Load the combo box using a subroutine.
Private Sub Load_lstCourses()
'Load the Student course listbox.
On Error GoTo Load_lstCourses_Err
Dim strItem As String
'Clearing the list box
lstCourses.Clear
'Load the lstCourse listbox
Set rsData = New ADODB.Recordset
strSQL = "SELECT * FROM usystblCourses WHERE (((usystblCourses.intDeleted)=False));"
rsData.Open strSQL, cnnMainDb, adOpenKeyset, adLockReadOnly
With rsData
Do While .EOF = False
lngCourse = !lngCourseID
strItem = !txtCourseName
lstCourses.AddItem strItem
lstCourses.ItemData(lstCourses.NewIndex) = lngCourse
.MoveNext
Loop
End With

Load_lstCourses_Exit:
On Error Resume Next
rsData.Close
Set rsData = Nothing
Exit Sub

Load_lstCourses_Err:
Msg = "Error in frmCourses - Load_lstCourses Subroutine. " & vbCr & _
"Error # " & Str(Err.Number) & " was generated by " _
& Err.Source & vbCr & Err.Description
MsgBox Msg, , "Error", Err.HelpFile, Err.HelpContext
Resume Load_lstCourses_Exit
End Sub

Step 2 - Code to delete the selected item from the combobox. I'm using MS-Access as my DB
Private Function Delete_Course(lngCourse As Long) As Boolean
On Error GoTo Delete_Course_Err
cnnMainDb.BeginTrans
strSQL = "UPDATE usystblCourses SET usystblCourses.dtmModified = Now(), " & _
"usystblCourses.intDeleted = True WHERE " & _
"(((usystblCourses.lngCourseID) = " & [lngCourse] & "));"

Set cmdChange = New ADODB.Command
Set cmdChange.ActiveConnection = cnnMainDb
cmdChange.CommandText = strSQL
Set rsData = New ADODB.Recordset
rsData.Open "usystblCourses", cnnMainDb, , , adCmdTable
cmdChange.Execute
DoEvents
Msg = "Do you wish to delete " & lstCourses & "?"
intResponse = MsgBox(Msg, vbYesNo + vbDefaultButton2 + vbQuestion, "Deleting Cosmetology Course")
If intResponse = vbYes Then
cnnMainDb.CommitTrans
Delete_Course = True
Else
cnnMainDb.RollbackTrans
Delete_Course = False
End If

Delete_Course_Exit:
On Error Resume Next
rsData.Close
Set rsData = Nothing
Exit Function

Delete_Course_Err:
Msg = "Error in frmCourse - Delete_Course function " & vbCr & _
"Error # " & Str(Err.Number) & " was generated by " _
& Err.Source & vbCr & Err.Description
MsgBox Msg, , "Error", Err.HelpFile, Err.HelpContext
Resume Delete_Course_Exit
End Function

Step 3 - Know which item you want to delete.
Private Sub cboCourses_Click()
lngCourse = CLng(cboCourses.ItemData(cboCourses.ListIndex))
End Sub

Step 4- Delete the item. I had a routine to check and make sure an item was selected before I deleted it.
Private Sub cmdDelete_Click()
If Is_Data_Present Then 'Checking routine
If Delete_Course(lngCourse) Then
Msg = lstCourses & " has been deleted."
MsgBox Msg, vbInformation, "Deleting item."
Else
Msg = lstCourses & " has NOT been deleted."
MsgBox Msg, vbInformation, "Deleting item."
End If
Else
Msg = "Before you can delete an item you must double click on the item name " & _
"you wish to delete. " & vbCr & _
"The item name must be placed into the Course Name box " & _
"before I can remove it from the database."
MsgBox Msg, vbInformation, "Deleting item."
End If
Call Load_lstCourses
txtCourse.Text = Empty
txtCourse.SetFocus
End Sub

I hope this helps,

Quecumber256
QuestionFREE .NET Related Material Pin
dotnetguide4-May-06 5:03
dotnetguide4-May-06 5:03 
AnswerRe: FREE .NET Related Material Pin
J4amieC4-May-06 5:39
J4amieC4-May-06 5:39 
Questiontextbox Pin
bawades4-May-06 4:17
bawades4-May-06 4:17 
AnswerRe: textbox Pin
Damoche104-May-06 5:13
Damoche104-May-06 5:13 
AnswerRe: textbox Pin
Member 28660305-May-06 2:49
Member 28660305-May-06 2:49 
Questionhide Pin
bawades4-May-06 3:49
bawades4-May-06 3:49 
QuestionASP Question Pin
alexfromto4-May-06 3:15
alexfromto4-May-06 3:15 
QuestionChange Scroll Bar Width? Pin
Kaditcher4-May-06 2:57
Kaditcher4-May-06 2:57 
Questioncrystal report help Pin
vipinpaliwal19804-May-06 1:28
vipinpaliwal19804-May-06 1:28 
Questionabout charts in powerpoint slides usig vb.net. Pin
jkrao4-May-06 1:25
jkrao4-May-06 1:25 
AnswerRe: about charts in powerpoint slides usig vb.net. Pin
Rabi@Dundas5-May-06 12:58
Rabi@Dundas5-May-06 12:58 
Questionserious problem in accessing data in datagrid in vb 6.0 Pin
sandeep_thakur4-May-06 0:46
sandeep_thakur4-May-06 0:46 
AnswerRe: serious problem in accessing data in datagrid in vb 6.0 Pin
Dave Kreskowiak4-May-06 2:06
mveDave Kreskowiak4-May-06 2:06 
AnswerRe: serious problem in accessing data in datagrid in vb 6.0 Pin
Dave Kreskowiak4-May-06 5:57
mveDave Kreskowiak4-May-06 5:57 
QuestionNeed help with VB.Net on splitting data grid windows Pin
Sithy4-May-06 0:22
Sithy4-May-06 0:22 
Questionirrgent help for Crystal Reports Pin
militiaware3-May-06 23:11
militiaware3-May-06 23:11 
AnswerRe: irrgent help for Crystal Reports Pin
J4amieC4-May-06 0:02
J4amieC4-May-06 0:02 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.