Results 1 to 14 of 14

Thread: listbox problem

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Feb 2006
    Posts
    73

    Arrow listbox problem

    hi experts
    i am working in VBA ,excel2003,sql-server 2000
    in my project there is a form has a listbox
    listbox contain the various user-name form the sql-table
    when i execute the delete query the user name must be remove from the list box() at run time ,How it is possible? the code is following PLZ help


    VB Code:
    1. Private Sub cmdDelete_Click()
    2.     Dim ADOConn As New ADODB.Connection
    3.     Dim ADORset As New ADODB.Recordset
    4.     Dim strQuery As String
    5.     Dim id_user As String
    6.     Dim Response As String
    7.     Dim ID As Integer
    8.  
    9.     If Me.txtUserID.Text = "" Then
    10.         MsgBox "Select a user to delete. ", vbInformation + vbOKOnly, "Delete Status"
    11.         Exit Sub
    12.     End If
    13.  
    14.     If Me.txtUserID.Text = "Tutorial" Or Me.txtUserID.Text = "Admin" Then
    15.         MsgBox "The record for UserID " + Chr$(34) + "Tutorial" + Chr$(34) + " and " + Chr$(34) + "Administrator" + Chr$(34) + " cannot be deleted. Select a new record and try again.", vbInformation + vbOKOnly, "Delete Status"
    16.         Exit Sub
    17.     End If
    18.     id_user = Me.txtUserID.Text
    19.     Set ADOConn = New ADODB.Connection
    20.     ADOConn.Open "Provider=sqloledb.1;Initial Catalog=Arvicon;uid=sa;pwd=; Data Source= localhost ;"
    21.     Set ADORset = New ADODB.Recordset
    22. Response = MsgBox("Are you sure you want to delete this record.", vbInformation + vbYesNo, "Delete Status")
    23.     If Response = 6 Then
    24.         strQuery = "Delete From tblUserData Where UserID='" & id_user & "'"
    25.         Set ADORset = ADOConn.Execute(strQuery)
    26.         MsgBox "Your record has been deleted successfully.", vbInformation + vbOKOnly, "Delete Status"
    27. ElseIf Response = 7 Then
    28.         cmdCancel_Click 'DoCmd.CancelEvent
    29.     End If
    30.  
    31.     ADOConn.Close
    32.     Set ADOConn = Nothing
    33. Exit Sub
    34. End Sub
    Last edited by pandey_om; Mar 17th, 2006 at 12:49 AM.

  2. #2
    Frenzied Member KGComputers's Avatar
    Join Date
    Dec 2005
    Location
    Cebu, PH
    Posts
    2,024

    Re: listbox problem

    Hi,

    Please put vbcode tags in your code to make it readable for the experts...Thanks....
    CodeBank: VB.NET & C#.NET | ASP.NET
    Programming: C# | VB.NET
    Blogs: Personal | Programming
    Projects: GitHub | jsFiddle
    ___________________________________________________________________________________

    Rating someone's post is a way of saying Thanks...

  3. #3

    Thread Starter
    Lively Member
    Join Date
    Feb 2006
    Posts
    73

    Re: listbox problem

    thanx for reply
    i have put vbcode tags in my code

  4. #4
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: listbox problem

    VB Code:
    1. Option Explicit
    2.  
    3. Private Declare Function SendMessage Lib "user32.dll" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, _
    4. ByVal wParam As Long, ByRef lParam As Any) As Long
    5.  
    6. Private Const LB_FINDSTRINGEXACT As Long = &H1A2
    7.  
    8. Private Sub Form_Load()
    9.     Dim intIndex As Integer
    10.     List1.AddItem "Text1"
    11.     Me.Text1.Text = "Text1"
    12.     intIndex = SendMessage(Me.List1.hwnd, LB_FINDSTRINGEXACT, 0&, ByVal Me.Text1.Text)
    13.     If intIndex > -1 Then
    14.         List1.RemoveItem intIndex
    15.     End If
    16. End Sub
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  5. #5

    Thread Starter
    Lively Member
    Join Date
    Feb 2006
    Posts
    73

    Re: listbox problem

    thanx for reply i m using the Itemremove method & it is working for me ,any correction can make u in this code
    VB Code:
    1. Private Sub cmdDelete_Click()
    2.     Dim ADOConn As New ADODB.Connection
    3.     Dim ADORset As New ADODB.Recordset
    4.     Dim strQuery As String
    5.     Dim id_user As String
    6.     Dim Response As String
    7.     Dim ID As Integer
    8.  
    9.     If Me.txtUserID.Text = "" Then
    10.         MsgBox "Select a user to delete. ", vbInformation + vbOKOnly, "Delete Status"
    11.         Exit Sub
    12.     End If
    13.  
    14.     If Me.txtUserID.Text = "Tutorial" Or Me.txtUserID.Text = "Admin" Then
    15.         MsgBox "The record for UserID " + Chr$(34) + "Tutorial" + Chr$(34) + " and " + Chr$(34) + "Administrator" + Chr$(34) + " cannot be deleted. Select a new record and try again.", vbInformation + vbOKOnly, "Delete Status"
    16.         Exit Sub
    17.     End If
    18.     id_user = Me.txtUserID.Text
    19.     Set ADOConn = New ADODB.Connection
    20.     ADOConn.Open "Provider=sqloledb.1;Initial Catalog=Arvicon;uid=sa;pwd=; Data Source= localhost ;"
    21.     Set ADORset = New ADODB.Recordset
    22.     'rst.FindFirst "UserId= " + Chr$(34) + id_user + Chr$(34)
    23.     'If Not rst.Bookmarkable = ID Then
    24.     'ID = rst.Fields("ID")
    25.     'End If
    26.     Response = MsgBox("Are you sure you want to delete this record.", vbInformation + vbYesNo, "Delete Status")
    27.     If Response = 6 Then
    28.         strQuery = "Delete From tblUserData Where UserID='" & id_user & "'"
    29.         Set ADORset = ADOConn.Execute(strQuery)
    30.        [B] ListBox1.RemoveItem ListBox1.ListIndex[/B] :eek2:
    31.         MsgBox "Your record has been deleted successfully.", vbInformation + vbOKOnly, "Delete Status"
    32.        
    33.     'If Not rst.BOF Then
    34.     'Previous_Record_Click
    35.     ' Else
    36.     'End If
    37.     ElseIf Response = 7 Then
    38.         cmdCancel_Click 'DoCmd.CancelEvent
    39.     End If
    40.  
    41.     ADOConn.Close
    42.     Set ADOConn = Nothing
    43. Exit Sub
    44. End Sub

  6. #6

    Thread Starter
    Lively Member
    Join Date
    Feb 2006
    Posts
    73

    Re: listbox problem

    can u show me what code i have to write if Response=7 it means user do not wana deletion
    how i cancele the deletion

  7. #7
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: listbox problem

    All this (ListBox1.RemoveItem ListBox1.ListIndex ) does is remove the item that is currently selected and not the one being referred to. You need to use my code in post #4 in order to find the matching text so you can retrieve the index number of it for the .RemoveItem method.
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  8. #8

    Thread Starter
    Lively Member
    Join Date
    Feb 2006
    Posts
    73

    Re: listbox problem

    hi Robdog888
    i am using your code but there is an error "Compile error-->Method or data member not found"
    VB Code:
    1. Private Sub cmdDelete_Click()
    2.     Dim ADOConn As New ADODB.Connection
    3.     Dim ADORset As New ADODB.Recordset
    4.     Dim strQuery As String
    5.     Dim id_user As String
    6.     Dim Response As String
    7.     Dim ID As Integer
    8.  
    9.     If Me.txtUserID.Text = "" Then
    10.         MsgBox "Select a user to delete. ", vbInformation + vbOKOnly, "Delete Status"
    11.         Exit Sub
    12.     End If
    13.  
    14.     If Me.txtUserID.Text = "Tutorial" Or Me.txtUserID.Text = "Admin" Then
    15.         MsgBox "The record for UserID " + Chr$(34) + "Tutorial" + Chr$(34) + " and " + Chr$(34) + "Administrator" + Chr$(34) + " cannot be deleted. Select a new record and try again.", vbInformation + vbOKOnly, "Delete Status"
    16.         Exit Sub
    17.     End If
    18.     id_user = Me.txtUserID.Text
    19.     Set ADOConn = New ADODB.Connection
    20.     ADOConn.Open "Provider=sqloledb.1;Initial Catalog=Arvicon;uid=sa;pwd=; Data Source= localhost ;"
    21.     Set ADORset = New ADODB.Recordset
    22.     Response = MsgBox("Are you sure you want to delete this record.", vbInformation + vbYesNo, "Delete Status")
    23.     If Response = 6 Then
    24.         strQuery = "Delete From tblUserData Where UserID='" & id_user & "'"
    25.         Set ADORset = ADOConn.Execute(strQuery)
    26.         intIndex = SendMessage[COLOR=DarkRed]([B]Me.ListBox1.hwnd,[/B]  :eek2:[/COLOR] 'error here  LB_FINDSTRINGEXACT, 0&, ByVal Me.txtUserID.Text)
    27.         If intIndex > -1 Then
    28.             ListBox1.RemoveItem intIndex
    29.         End If
    30.  
    31.         'ListBox1.RemoveItem ListBox1.ListIndex
    32.         MsgBox "Your record has been deleted successfully.", vbInformation + vbOKOnly, "Delete Status"
    33.     ElseIf Response = 7 Then
    34.         cmdCancel_Click 'DoCmd.CancelEvent
    35.     End If
    36.     ADOConn.Close
    37.     Set ADOConn = Nothing
    38. Exit Sub
    39. End Sub

  9. #9

    Thread Starter
    Lively Member
    Join Date
    Feb 2006
    Posts
    73

    Re: listbox problem

    error in the Me.listbox1.hwnd
    hwnd is reason of error

  10. #10
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: listbox problem

    Me.ListBox1.hwnd with the name of YOUR listbox.
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  11. #11

    Thread Starter
    Lively Member
    Join Date
    Feb 2006
    Posts
    73

    Re: listbox problem

    name of MY list box is listbox1
    but error is present

  12. #12

    Thread Starter
    Lively Member
    Join Date
    Feb 2006
    Posts
    73

    Re: listbox problem

    i think there is no reference of dll
    how i can referenced ,there is no dll reference in Tool-->reference
    which dll i frefer on browing, there are various dll file i saw

  13. #13

    Thread Starter
    Lively Member
    Join Date
    Feb 2006
    Posts
    73

    Re: listbox problem

    when i reference user32.dll
    error msg "u can't refer this file " i m using excel 2003
    help plz

  14. #14
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: listbox problem

    You are mising the rest of the code from post #4. The API declaration and Constant declaration too.

    Is this behind a UserForm then since its Excel VBA?
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width