Results 1 to 21 of 21

Thread: Frequently Asked VB Questions

Threaded View

  1. #1

    Thread Starter
    Software Carpenter dee-u's Avatar
    Join Date
    Feb 2005
    Location
    Pinas
    Posts
    11,127

    Arrow Frequently Asked VB Questions

    In order to make this thread as tidy as possible pls. PM me all your comments, suggestions, improvements, modifications.... And pls. post only those that are only truly "Frequently Asked Questions".... Thanks!

    1. How to read/write ini?

    VB Code:
    1. Option Explicit
    2.  
    3. 'declares for ini controlling
    4. Private Declare Function GetPrivateProfileSection Lib "kernel32" Alias "GetPrivateProfileSectionA" (ByVal lpAppName As String, ByVal lpReturnedString As String, ByVal nSize As Long, ByVal lpFileName As String) As Long
    5.  
    6. Private Declare Function GetPrivateProfileString Lib "kernel32" Alias "GetPrivateProfileStringA" (ByVal lpApplicationName As String, ByVal lpKeyName As Any, ByVal lpDefault As String, ByVal
    7. lpReturnedString As String, ByVal nSize As Long, ByVal lpFileName As String) As Long
    8.  
    9. Private Declare Function WritePrivateProfileSection Lib "kernel32" Alias "WritePrivateProfileSectionA" (ByVal lpAppName As String, ByVal lpString As String, ByVal lpFileName As String) As Long
    10.  
    11. Private Declare Function WritePrivateProfileString Lib "kernel32" Alias "WritePrivateProfileStringA" (ByVal lpApplicationName As String, ByVal lpKeyName As Any, ByVal lpString As Any, ByVal lpFileName As String) As Long
    12.  
    13. Private Sub Form_Load()
    14. On Error Resume Next
    15.  
    16. Dim File As String, OFLen As Double,  Str As String
    17.  
    18. File = "C:\temp.txt"
    19. OFLen = FileLen(File)
    20.  
    21. 'write few example sections:
    22. WriteIniSection File, "Test1", ""
    23. WriteIniSection File, "Test2", "Here shoud be found some text"
    24.  
    25. 'write few ini strings
    26. WriteIni File, "Test3", "Ini1", "This is ini 1"
    27. WriteIni File, "Test1", "Ini2", "This is ini 2"
    28.  
    29. 'inform we're written the data
    30. MsgBox Format((FileLen(File) - OFLen) / 1024, "0.00") & " KB data written to " & Chr(34) & File & Chr(34)
    31.  
    32. 'read the ini file
    33. Str = Str & "Test2 section: " & vbTab & ReadIniSection(File, "Test2") & vbCrLf
    34. Str = Str & "Test1 section: " & vbTab & ReadIniSection(File, "Test1") & vbCrLf
    35. Str = Str & "Ini1 string: " & vbTab & ReadIni(File, "Test3", "Ini1") & vbCrLf
    36. Str = Str & "Ini2 string: " & vbTab & ReadIni(File, "Test1", "Ini2") & vbCrLf
    37.  
    38. 'show data
    39. MsgBox Str
    40.  
    41. 'end application
    42. End
    43.  
    44. End Sub
    45.  
    46. '// INI CONTROLLING PROCEDURES
    47.  
    48. 'reads ini string
    49. Public Function ReadIni(Filename As String, Section As String, Key As String) As String
    50. Dim RetVal As String * 255, v As Long
    51. v = GetPrivateProfileString(Section, Key, "", RetVal, 255, Filename)
    52. ReadIni = Left(RetVal, v - 1)
    53. End Function
    54.  
    55. 'reads ini section
    56. Public Function ReadIniSection(Filename As String, Section As String) As String
    57. Dim RetVal As String * 255, v As Long
    58. v = GetPrivateProfileSection(Section, RetVal, 255, Filename)
    59. ReadIniSection = Left(RetVal, v - 1)
    60. End Function
    61.  
    62. 'writes ini
    63. Public Sub WriteIni(Filename As String, Section As String, Key As String, Value As String)
    64. WritePrivateProfileString Section, Key, Value, Filename
    65. End Sub
    66.  
    67. 'writes ini section
    68. Public Sub WriteIniSection(Filename As String, Section As String, Value As String)
    69. WritePrivateProfileSection Section, Value, Filename
    70. End Sub
    2. How to send e-mail?
    Have a look at this official FAQ article: How do I send an email using Outlook

    3.How to work with registry?
    VB Code:
    1. 'Savesetting
    2. Private Sub Form_Unload(Cancel As Integer)
    3.    SaveSetting "RegCust", "Startup", "Backup", strDate
    4.    SaveSetting "RegCust", "Startup", "LastEntry",intLastEntry
    5. End Sub
    6. 'GetSetting
    7. Private Sub Form_Load()
    8.    Dim intLastEntry As Integer
    9.    intLastEntry = GetSetting("RegCust", "Startup", _
    10.    "LastEntry", "0")
    11. End Sub
    12. 'DeleteSetting
    13. Private Sub cmdDelKey_Click()
    14.    DeleteSetting "RegCust", "StartUp", "LastEntry"
    15. End Sub
    16. Private Sub cmdDelSection_Click()
    17.    DeleteSetting "RegCust", "StartUp"
    18. End Sub
    19. Private Sub cmdUnInstall_Click()
    20.    DeleteSetting "RegCust"
    21. End Sub
    4. How to open Password protected Access db?
    VB Code:
    1. 'connection to secure database
    2. Dim Cnn As ADODB.Connection
    3.  
    4. 'In the place where you want to establish your connection, such
    5. 'as the Initialize event of a class module, enter the following:
    6.  
    7. Dim strConnect As String
    8. Set Cnn = New ADODB.Connection
    9.  
    10. 'Substitute your own User IDs, Password, Data Source, and System
    11. 'database in the connection string below
    12.     strConnect = "Provider=Microsoft.Jet.OLEDB.4.0;" & _
    13.         "Password=MyPassword;User ID=Administrator;" & _
    14.         "Data Source=C:\AccessDBs\DB1.mdb;" & _
    15.         "Persist Security Info=True;" & _
    16.         "Jet OLEDB:System database=C:\AccessDBs\system.mdw"
    17.  
    18. 'if you don't need you can eliminate the last line in connection string
    19.  
    20.     With Cnn
    21.         .CursorLocation = adUseClient
    22.         .Open strConnect
    23.     End With
    5. How to save image in db?
    Have a look at this official FAQ article: Database - How can I store images (or other files) in a database?

    6. How to write/read text file?
    VB Code:
    1. 'write
    2. Option Explicit
    3. 'Set a reference to Microsoft Scripting Runtime
    4. Private Sub Command1_Click()
    5.     Dim fs As FileSystemObject
    6.     Dim ts As TextStream
    7.      
    8.     Set fs = New FileSystemObject
    9.     'To write
    10.     Set ts = fs.OpenTextFile("C:\mytestfile.txt", ForWriting, True)
    11.     ts.WriteLine "I Love"
    12.     ts.WriteLine "VB Forums"
    13.     ts.Close
    14.      
    15.     'To Read
    16.     If fs.FileExists("C:\mytestfile.txt") Then
    17.         Set ts = fs.OpenTextFile("C:\mytestfile.txt")
    18.          
    19.         Do While Not ts.AtEndOfStream
    20.             MsgBox ts.ReadLine
    21.         Loop
    22.         ts.Close
    23.     End If
    24.     Set ts = Nothing
    25.     Set fs = Nothing
    26. End Sub
    27.  
    28. 'read
    29. Function FileText(ByVal filename As String) As String
    30.     Dim handle As Integer
    31.      
    32.     ' ensure that the file exists
    33.     If Len(Dir$(filename)) = 0 Then
    34.         Err.Raise 53  ' File not found
    35.     End If
    36.      
    37.     ' open in binary mode
    38.     handle = FreeFile
    39.     Open filename$ For Binary As #handle
    40.     ' read the string and close the file
    41.     FileText = Space$(LOF(handle))
    42.     Get #handle, , FileText
    43.     Close #handle
    44. End Function

    7. How to fix invalid characters in sql criteria?
    VB Code:
    1. Crit="Mark's"
    2. strSQL = "SELECT * FROM MyTable WHERE ComField ='" & CleanText(Crit) & "'"
    3. Public Function CleanText(strIn As String) As String
    4.     On Error GoTo VBError
    5.     Dim iAcnt As Long
    6.     Dim strString As String
    7.     Dim vLimit As Long
    8.     vLimit = Len(strIn)
    9.     For iAcnt = 1 To vLimit
    10.         Select Case Asc(Mid$(strIn, iAcnt, 1))
    11.         Case 10, 13
    12.             strString = strString & Mid$(strIn, iAcnt, 1)
    13.         Case 124
    14.             strString = strString & "!"
    15.         Case 39
    16.             strString = strString & "''"
    17.         Case 34
    18.             strString = strString & """"
    19.         Case Is < 32
    20.             strString = strString & " "
    21.         Case Is > 126
    22.             strString = strString & " "
    23.         Case Else
    24.             strString = strString & Mid$(strIn, iAcnt, 1)
    25.         End Select
    26.     Next
    27.     CleanText = strString
    28.     CleanText = Trim$(CleanText)
    29. Exit Function
    30. VBError:
    31.     MsgBox "VBError in Sub Parse_SQL_Text : " & Err.Number & " - " & Err.Description
    32.     Resume Next
    33. End Function

    8. How to dynamically add controls?
    VB Code:
    1. Dim WithEvents vTextBox As TextBox
    2. Set vTextBox = Controls.Add("vb.textbox", "DeeU")
    Last edited by dee-u; Dec 21st, 2008 at 01:15 AM.
    Regards,


    As a gesture of gratitude please consider rating helpful posts. c",)

    Some stuffs: Mouse Hotkey | Compress file using SQL Server! | WPF - Rounded Combobox | WPF - Notify Icon and Balloon | NetVerser - a WPF chatting system

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