473,406 Members | 2,352 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

VB Tips & Tricks

sashi
1,754 Expert 1GB
Hi everyone,

Here are some VB related tips & tricks, hope it helps. Good luck & Take care.

Important

This thread is closed for general posting. Please start a new thread if you have a question or comment.

ADMINISTRATOR
Dec 2 '06 #1
17 23249
sashi
1,754 Expert 1GB
Connection string More samples

MsAccess - Connection String

Standard security
Expand|Select|Wrap|Line Numbers
  1. oConn.Open "Provider=Microsoft.Jet.OLEDB.4.0;" & _
  2.            "Data Source=c:\somepath\myDb.mdb;" 
  3.  
If using a Workgroup (System Database)
Expand|Select|Wrap|Line Numbers
  1. oConn.Open "Provider=Microsoft.Jet.OLEDB.4.0;" & _
  2.            "Data Source=c:\somepath\mydb.mdb;" & _ 
  3.            "Jet OLEDB:System Database=MySystem.mdw", _
  4.            "myUsername", "myPassword" 
  5.  
If MDB has a database password
Expand|Select|Wrap|Line Numbers
  1. oConn.Open "Provider=Microsoft.Jet.OLEDB.4.0;" & _
  2.            "Data Source=c:\somepath\mydb.mdb;" & _ 
  3.            "Jet OLEDB:Database Password=MyDbPassword", _
  4.            "myUsername", "myPassword"
  5.  
If want to open up the MDB exclusively
Expand|Select|Wrap|Line Numbers
  1. oConn.Mode = adModeShareExclusive
  2. oConn.Open "Provider=Microsoft.Jet.OLEDB.4.0;" & _
  3.            "Data Source=c:\somepath\myDb.mdb;"  
  4.  
If MDB is located on a network share
Expand|Select|Wrap|Line Numbers
  1. oConn.Open "Provider=Microsoft.Jet.OLEDB.4.0;" & _
  2.            "Data Source=\\myServer\myShare\myPath\myDb.mdb"
  3.  
ODBC - Open Database Connectivity

DSN
Expand|Select|Wrap|Line Numbers
  1. oConn.Open "DSN=mySystemDSN;" & _ 
  2.            "Uid=myUsername;" & _ 
  3.            "Pwd=myPassword"
  4.  
File DSN
Expand|Select|Wrap|Line Numbers
  1. oConn.Open "FILEDSN=c:\somepath\mydb.dsn;" & _ 
  2.            "Uid=myUsername;" & _
  3.            "Pwd=myPassword"
  4.  
MsSQL Server - Connection String

For Standard Security
Expand|Select|Wrap|Line Numbers
  1. oConn.Open "Provider=sqloledb;" & _ 
  2.            "Data Source=myServerName;" & _
  3.            "Initial Catalog=myDatabaseName;" & _
  4.            "User Id=myUsername;" & _
  5.            "Password=myPassword"
  6.  
OR

Expand|Select|Wrap|Line Numbers
  1. oConn.Open "Provider=sqloledb;" & _ 
  2.            "Server=myServerName;" & _
  3.            "Database=myDatabaseName;" & _
  4.            "User Id=myUsername;" & _
  5.            "Password=myPassword"
  6.  
For a Trusted Connection
Expand|Select|Wrap|Line Numbers
  1. oConn.Open "Provider=sqloledb;" & _
  2.            "Data Source=myServerName;" & _
  3.            "Initial Catalog=myDatabaseName;" & _
  4.            "Integrated Security=SSPI"
  5.  
To connect to a "Named Instance"
Expand|Select|Wrap|Line Numbers
  1. oConn.Open "Provider=sqloledb;" & _
  2.            "Data Source=myServerName\myInstanceName;" & _
  3.            "Initial Catalog=myDatabaseName;" & _
  4.            "User Id=myUsername;" & _
  5.            "Password=myPassword"
  6.  
Note: In order to connect to a SQL Server 2000 "named instance", you must have MDAC 2.6 (or greater) installed.

To Prompt user for username and password
Expand|Select|Wrap|Line Numbers
  1. oConn.Provider = "sqloledb"
  2. oConn.Properties("Prompt") = adPromptAlways
  3. oConn.Open "Data Source=myServerName;" & _
  4.            "Initial Catalog=myDatabaseName" 
  5.  
To connect to SQL Server running on the same computer
Expand|Select|Wrap|Line Numbers
  1. oConn.Open "Provider=sqloledb;" & _
  2.            "Data Source=(local);" & _
  3.            "Initial Catalog=myDatabaseName;" & _
  4.            "User ID=myUsername;" & _
  5.            "Password=myPassword"
  6.  
To connect to SQL Server running on a remote computer (via an IP address)
Expand|Select|Wrap|Line Numbers
  1. oConn.Open "Provider=sqloledb;" & _
  2.            "Network Library=DBMSSOCN;" & _
  3.            "Data Source=xxx.xxx.xxx.xxx,1433;" & _
  4.            "Initial Catalog=myDatabaseName;" & _
  5.            "User ID=myUsername;" & _
  6.            "Password=myPassword"
  7.  
Dec 2 '06 #2
sashi
1,754 Expert 1GB
Format database date

Expand|Select|Wrap|Line Numbers
  1. Public Function FormatDate(ByVal vdtDate As Date) As String
  2. Dim dtNullDate As Date
  3. FormatDate = "NULL"
  4.  
  5.   If vdtDate = dtNullDate Then Exit Function
  6.   If DatePart("h", vdtDate) = 0 And DatePart("n", vdtDate) = 0 And     DatePart("s", vdtDate) = 0 Then
  7.     FormatDate = "{d '" & Format$(vdtDate, "yyyy-mm-dd") & "'}"
  8.   Else
  9.     FormatDate = "{ts '" & Format$(vdtDate, "yyyy-mm-dd hh:nn:ss") & "'}"
  10.   End If
  11. End Function
  12.  
Dec 4 '06 #3
sashi
1,754 Expert 1GB
Apostrophe

Have you ever tried to send a string variable to MS Access that had apostrophes embedded within an SQL Statement? If YES you will get a run time ERROR. Here is your solution, a function that formats the variable before sending it to the database.

Expand|Select|Wrap|Line Numbers
  1. Public Function Apostrophe(sFieldString As String) As String
  2.   If InStr(sFieldString, "'") Then
  3.     Dim iLen As Integer
  4.     Dim ii As Integer
  5.     Dim apostr As Integer
  6.     iLen = Len(sFieldString)
  7.     ii = 1
  8.  
  9.       Do While ii <= iLen
  10.         If Mid(sFieldString, ii, 1) = "'" Then
  11.           apostr = ii
  12.           sFieldString = Left(sFieldString, apostr) & "'" & _
  13.           Right(sFieldString, iLen - apostr)
  14.           iLen = Len(sFieldString)
  15.           ii = ii + 1
  16.         End If
  17.       ii = ii + 1
  18.       Loop
  19.   End If
  20.  
  21.   Apostrophe = sFieldString
  22. End Function
  23.  
Dec 4 '06 #4
sashi
1,754 Expert 1GB
ShellExecute

Expand|Select|Wrap|Line Numbers
  1. 'Module code - modShellExecute
  2.  
  3. Public Declare Function ShellExecute Lib "shell32.dll" Alias "ShellExecuteA" (ByVal hWnd As Long, ByVal lpOperation As String, ByVal lpFile As String, ByVal lpParameters As String, ByVal lpDirectory As String, ByVal nShowCmd As Long) As Long
  4.  
  5. 'vbHide = 0
  6. 'vbNormalFocus = 1
  7. 'vbMinimizedFocus = 2
  8. 'vbMaximizedFocus = 3
  9. 'vbNormalNoFocus = 4
  10. 'vbMinimizedNoFocus = 6
  11.  
  12. Public Enum vbWindowsState
  13.     Hide = 0
  14.     NormalFocus = 1
  15.     MinimizedFocus = 2
  16.     MaximizedFocus = 3
  17.     NormalNoFocus = 4
  18.     MinimizedNoFocus = 6
  19. End Enum
  20.  
  21. Public Function OpenApplication(ByVal strOperation As String, _
  22.                                 ByVal strApplicationName As String, _
  23.                                 ByVal strParameter As String, _
  24.                                 ByVal strApplicationDirectory As String, _
  25.                                 ByVal WindowsState As vbWindowsState) As Boolean
  26.  
  27. Dim nWindowsState As Integer
  28.  
  29.     Select Case WindowsState
  30.         Case 0
  31.             nWindowsState = vbHide
  32.         Case 1
  33.             nWindowsState = vbNormalFocus
  34.         Case 2
  35.             nWindowsState = vbMinimizedFocus
  36.         Case 3
  37.             nWindowsState = vbMaximizedFocus
  38.         Case 4
  39.             nWindowsState = vbNormalNoFocus
  40.         Case 6
  41.             nWindowsState = vbMinimizedNoFocus
  42.     End Select
  43.  
  44.     ShellExecute 0&, strOperation, strApplicationName, strParameter, strApplicationDirectory, WindowsState
  45. End Function
  46.  
  47. 'Form code - frmShellExecute
  48.  
  49. Private Sub cmdShellExecute_Click()
  50.     modShellExecute.OpenApplication vbNullString, "The Application", vbNullString, vbNullString, MaximizedFocus
  51. End Sub
  52.  
Dec 4 '06 #5
sashi
1,754 Expert 1GB
Shut down Windows

Expand|Select|Wrap|Line Numbers
  1. 'Module code - modShutdown
  2.  
  3. ' Shutdown Flags
  4. Const EWX_LOGOFF = 0
  5. Const EWX_SHUTDOWN = 1
  6. Const EWX_REBOOT = 2
  7. Const EWX_FORCE = 4
  8. Const SE_PRIVILEGE_ENABLED = &H2
  9. Const TokenPrivileges = 3
  10. Const TOKEN_ASSIGN_PRIMARY = &H1
  11. Const TOKEN_DUPLICATE = &H2
  12. Const TOKEN_IMPERSONATE = &H4
  13. Const TOKEN_QUERY = &H8
  14. Const TOKEN_QUERY_SOURCE = &H10
  15. Const TOKEN_ADJUST_PRIVILEGES = &H20
  16. Const TOKEN_ADJUST_GROUPS = &H40
  17. Const TOKEN_ADJUST_DEFAULT = &H80
  18. Const SE_SHUTDOWN_NAME = "SeShutdownPrivilege"
  19. Const ANYSIZE_ARRAY = 1
  20. Private Type LARGE_INTEGER
  21.     lowpart As Long
  22.     highpart As Long
  23. End Type
  24. Private Type Luid
  25.     lowpart As Long
  26.     highpart As Long
  27. End Type
  28. Private Type LUID_AND_ATTRIBUTES
  29.     'pLuid As Luid
  30.     pLuid As LARGE_INTEGER
  31.     Attributes As Long
  32. End Type
  33. Private Type TOKEN_PRIVILEGES
  34.     PrivilegeCount As Long
  35.     Privileges(ANYSIZE_ARRAY) As LUID_AND_ATTRIBUTES
  36. End Type
  37. Private Declare Function InitiateSystemShutdown Lib "advapi32.dll" Alias "InitiateSystemShutdownA" (ByVal lpMachineName As String, ByVal lpMessage As String, ByVal dwTimeout As Long, ByVal bForceAppsClosed As Long, ByVal bRebootAfterShutdown As Long) As Long
  38. Private Declare Function OpenProcessToken Lib "advapi32.dll" (ByVal ProcessHandle As Long, ByVal DesiredAccess As Long, TokenHandle As Long) As Long
  39. Private Declare Function GetCurrentProcess Lib "kernel32" () As Long
  40. Private Declare Function LookupPrivilegeValue Lib "advapi32.dll" Alias "LookupPrivilegeValueA" (ByVal lpSystemName As String, ByVal lpName As String, lpLuid As LARGE_INTEGER) As Long
  41. Private Declare Function AdjustTokenPrivileges Lib "advapi32.dll" (ByVal TokenHandle As Long, ByVal DisableAllPrivileges As Long, NewState As TOKEN_PRIVILEGES, ByVal BufferLength As Long, PreviousState As TOKEN_PRIVILEGES, ReturnLength As Long) As Long
  42. Private Declare Function GetComputerName Lib "kernel32" Alias "GetComputerNameA" (ByVal lpBuffer As String, nSize As Long) As Long
  43. Private Declare Function GetLastError Lib "kernel32" () As Long
  44.  
  45. Public Function InitiateShutdown(ByVal Machine As String, _
  46.                                  Optional Force As Variant, _
  47.                                  Optional Restart As Variant, _
  48.                                  Optional AllowLocalShutdown As Variant, _
  49.                                  Optional Delay As Variant, _
  50.                                  Optional Message As Variant) As Boolean
  51.  
  52.     Dim hProc As Long
  53.     Dim OldTokenStuff As TOKEN_PRIVILEGES
  54.     Dim OldTokenStuffLen As Long
  55.     Dim NewTokenStuff As TOKEN_PRIVILEGES
  56.     Dim NewTokenStuffLen As Long
  57.     Dim pSize As Long
  58.     If IsMissing(Force) Then Force = False
  59.     If IsMissing(Restart) Then Restart = True
  60.     If IsMissing(AllowLocalShutdown) Then AllowLocalShutdown = False
  61.     If IsMissing(Delay) Then Delay = 0
  62.     If IsMissing(Message) Then Message = ""
  63.     'Make sure the Machine-name doesn't start with '\\'
  64.     If InStr(Machine, "\\") = 1 Then
  65.         Machine = Right(Machine, Len(Machine) - 2)
  66.     End If
  67.     'check if it's the local machine that's going to be shutdown
  68.     If (LCase(GetMachineName) = LCase(Machine)) Then
  69.         'may we shut this computer down?
  70.         If AllowLocalShutdown = False Then Exit Function
  71.         'open access token
  72.         If OpenProcessToken(GetCurrentProcess(), TOKEN_ADJUST_PRIVILEGES Or TOKEN_QUERY, hProc) = 0 Then
  73.             MsgBox "OpenProcessToken Error: " & GetLastError()
  74.             Exit Function
  75.         End If
  76.         'retrieve the locally unique identifier to represent the Shutdown-privilege name
  77.         If LookupPrivilegeValue(vbNullString, SE_SHUTDOWN_NAME, OldTokenStuff.Privileges(0).pLuid) = 0 Then
  78.             MsgBox "LookupPrivilegeValue Error: " & GetLastError()
  79.             Exit Function
  80.         End If
  81.         NewTokenStuff = OldTokenStuff
  82.         NewTokenStuff.PrivilegeCount = 1
  83.         NewTokenStuff.Privileges(0).Attributes = SE_PRIVILEGE_ENABLED
  84.         NewTokenStuffLen = Len(NewTokenStuff)
  85.         pSize = Len(NewTokenStuff)
  86.         'Enable shutdown-privilege
  87.         If AdjustTokenPrivileges(hProc, False, NewTokenStuff, NewTokenStuffLen, OldTokenStuff, OldTokenStuffLen) = 0 Then
  88.             MsgBox "AdjustTokenPrivileges Error: " & GetLastError()
  89.             Exit Function
  90.         End If
  91.         'initiate the system shutdown
  92.         If InitiateSystemShutdown("\\" & Machine, Message, Delay, Force, Restart) = 0 Then
  93.             Exit Function
  94.         End If
  95.         NewTokenStuff.Privileges(0).Attributes = 0
  96.         'Disable shutdown-privilege
  97.         If AdjustTokenPrivileges(hProc, False, NewTokenStuff, Len(NewTokenStuff), OldTokenStuff, Len(OldTokenStuff)) = 0 Then
  98.             Exit Function
  99.         End If
  100.     Else
  101.         'initiate the system shutdown
  102.         If InitiateSystemShutdown("\\" & Machine, Message, Delay, Force, Restart) = 0 Then
  103.             Exit Function
  104.         End If
  105.     End If
  106.     InitiateShutdown = True
  107. End Function
  108.  
  109. Function GetMachineName() As String
  110.     Dim sLen As Long
  111.     'create a buffer
  112.     GetMachineName = Space(100)
  113.     sLen = 100
  114.     'retrieve the computer name
  115.     If GetComputerName(GetMachineName, sLen) Then
  116.         GetMachineName = Left(GetMachineName, sLen)
  117.     End If
  118. End Function
  119.  
  120.  
  121. 'Form code - frmShutdown
  122.  
  123. Private Sub cmdShutdownNow_Click()
  124.     modShutdown.InitiateShutdown GetMachineName, True, False, True, 60, "Message to state reason for shutdown!"
  125. End Sub
  126.  
Dec 4 '06 #6
sashi
1,754 Expert 1GB
Generate Random Password

Expand|Select|Wrap|Line Numbers
  1. 'Form code - frmPasswordGenerate
  2.  
  3. Private Declare Function GetTickCount Lib "kernel32" () As Long
  4.  
  5. Public Function PassGen(nLen As Integer)
  6. Dim range As Collection
  7. Dim ivalue, icount, iLen As Long
  8. Dim pass As String
  9.  
  10.     Set range = New Collection
  11.     range.Add ("0")
  12.     range.Add ("1")
  13.     range.Add ("2")
  14.     range.Add ("3")
  15.     range.Add ("4")
  16.     range.Add ("5")
  17.     range.Add ("6")
  18.     range.Add ("7")
  19.     range.Add ("8")
  20.     range.Add ("9")
  21.  
  22.     icount = 0
  23.     ivalue = 0
  24.     iLen = range.Count
  25.  
  26.     Do Until icount = nLen
  27.       Randomize
  28.       ivalue = CByte(Mid(CStr(Rnd(GetTickCount)), 3, 2))
  29.        If ivalue > 0 And ivalue <= iLen Then
  30.           icount = icount + 1
  31.           pass = pass & range(ivalue)
  32.        End If
  33.     Loop
  34.  
  35. PassGen = pass
  36. End Function
  37.  
  38. Private Sub cmdGeneratePassword_Click()
  39.     MsgBox PassGen(8)
  40. End Sub
  41.  
Dec 4 '06 #7
sashi
1,754 Expert 1GB
BLOB - Save image to database

Expand|Select|Wrap|Line Numbers
  1. Dim CN As New ADODB.Connection
  2. Dim RS As ADODB.Recordset
  3. Dim DataFile As Integer, Fl As Long, Chunks As Integer
  4. Dim Fragment As Integer, Chunk() As Byte, i As Integer, FileName As String
  5.  
  6. Private Const ChunkSize As Integer = 16384
  7. Private Const conChunkSize = 100
  8.  
  9. Private Sub cmdSave_Click()
  10.     CN.Open "Provider=SQLOLEDB.1;Persist Security Info=False;User ID=sa;Initial Catalog=Pubs;Data Source=Test"
  11.     Dim strSQL As String
  12.  
  13.     strSQL = "SELECT * FROM pub_info where pub_id = '9999'"
  14.     RS.Open strSQL, CN, adOpenForwardOnly, adLockOptimistic
  15.  
  16.     RS.AddNew
  17.       SavePicture
  18.     RS.Update
  19.  
  20.     Set RS = Nothing
  21.     Set RS = New Recordset
  22. End Sub
  23.  
  24. Private Sub SavePicture()
  25.     Dim strFileNm As String
  26.     DataFile = 1
  27.     Open strFileNm For Binary Access Read As DataFile
  28.         Fl = LOF(DataFile)   ' Length of data in file
  29.         If Fl = 0 Then Close DataFile: Exit Sub
  30.         Chunks = Fl \ ChunkSize
  31.         Fragment = Fl Mod ChunkSize
  32.         ReDim Chunk(Fragment)
  33.         Get DataFile, , Chunk()
  34.         RS!logo.AppendChunk Chunk()
  35.         ReDim Chunk(ChunkSize)
  36.         For i = 1 To Chunks
  37.             Get DataFile, , Chunk()
  38.             RS!logo.AppendChunk Chunk()
  39.         Next i
  40.     Close DataFile
  41. End Sub
  42.  
Dec 4 '06 #8
sashi
1,754 Expert 1GB
BLOB - Retieve image stored in database

Expand|Select|Wrap|Line Numbers
  1. Dim CN As New ADODB.Connection
  2. Dim RS As ADODB.Recordset
  3. Dim DataFile As Integer, Fl As Long, Chunks As Integer
  4. Dim Fragment As Integer, Chunk() As Byte, i As Integer, FileName As String
  5.  
  6. Private Const ChunkSize As Integer = 16384
  7. Private Const conChunkSize = 100
  8.  
  9. Private Sub Form_Load()
  10.     CN.Open "Provider=SQLOLEDB.1;Persist Security Info=False;User ID=sa;Initial Catalog=Pubs;Data Source=Test"
  11.     Dim strsql As String
  12.  
  13.     strsql = "SELECT * FROM pub_info where pub_id = '9999'"
  14.     RS.Open strsql, CN, adOpenForwardOnly, adLockReadOnly
  15.       ShowPic
  16.     Set RS = Nothing
  17.     Set RS = New Recordset
  18. End Sub
  19.  
  20. Private Sub ShowPic()
  21.     DataFile = 1
  22.     Open "pictemp" For Binary Access Write As DataFile
  23.         Fl = RS!logo.ActualSize ' Length of data in file
  24.         If Fl = 0 Then Close DataFile: Exit Sub
  25.         Chunks = Fl \ ChunkSize
  26.         Fragment = Fl Mod ChunkSize
  27.         ReDim Chunk(Fragment)
  28.         Chunk() = RS!logo.GetChunk(Fragment)
  29.         Put DataFile, , Chunk()
  30.         For i = 1 To Chunks
  31.             ReDim Buffer(ChunkSize)
  32.             Chunk() = RS!logo.GetChunk(ChunkSize)
  33.             Put DataFile, , Chunk()
  34.         Next i
  35.     Close DataFile
  36.     FileName = "pictemp"
  37.     Picture1.Picture = LoadPicture(FileName)
  38. End Sub
  39.  
Dec 4 '06 #9
sashi
1,754 Expert 1GB
Disk free space

'Declarations

Expand|Select|Wrap|Line Numbers
  1. Option Explicit
  2.  
  3. Private Type LARGE_INTEGER
  4.     lowpart As Long
  5.     highpart As Long
  6. End Type
  7. Private Declare Function GetDiskFreeSpaceEx Lib "kernel32" Alias "GetDiskFreeSpaceExA" (ByVal lpRootPathName As String, lpFreeBytesAvailableToCaller As LARGE_INTEGER, lpTotalNumberOfBytes As LARGE_INTEGER, lpTotalNumberOfFreeBytes As LARGE_INTEGER) As Long
  8.  
  9.  
'Code

Expand|Select|Wrap|Line Numbers
  1. Public Function GetDiskSpace(sDrive As String) As String
  2.     Dim lResult As Long
  3.     Dim liAvailable As LARGE_INTEGER
  4.     Dim liTotal As LARGE_INTEGER
  5.     Dim liFree As LARGE_INTEGER
  6.     Dim dblAvailable As Double
  7.     Dim dblTotal As Double
  8.     Dim dblFree As Double
  9.     If Right(sDrive, 1) <> "" Then sDrive = sDrive & ""
  10.     'Determine the Available Space, Total Size and Free Space of a drive
  11.     lResult = GetDiskFreeSpaceEx(sDrive, liAvailable, liTotal, liFree)
  12.  
  13.     'Convert the return values from LARGE_INTEGER to doubles
  14.     dblAvailable = CLargeInt(liAvailable.lowpart, liAvailable.highpart)
  15.     dblTotal = CLargeInt(liTotal.lowpart, liTotal.highpart)
  16.     dblFree = CLargeInt(liFree.lowpart, liFree.highpart)
  17.  
  18.     'Display the results
  19.     GetDiskSpace = "Available Space on " & sDrive & ":  " & dblAvailable & " bytes (" & _
  20.                 Format(dblAvailable / 1024 ^ 3, "0.00") & " G) " & vbCr & _
  21.                 "Total Space on " & sDrive & ":      " & dblTotal & " bytes (" & _
  22.                 Format(dblTotal / 1024 ^ 3, "0.00") & " G) " & vbCr & _
  23.                 "Free Space on " & sDrive & ":       " & dblFree & " bytes (" & _
  24.                 Format(dblFree / 1024 ^ 3, "0.00") & " G) "
  25. End Function
  26.  
  27. Private Function CLargeInt(Lo As Long, Hi As Long) As Double
  28.     'This function converts the LARGE_INTEGER data type to a double
  29.     Dim dblLo As Double, dblHi As Double
  30.  
  31.     If Lo < 0 Then
  32.         dblLo = 2 ^ 32 + Lo
  33.     Else
  34.         dblLo = Lo
  35.     End If
  36.  
  37.     If Hi < 0 Then
  38.         dblHi = 2 ^ 32 + Hi
  39.     Else
  40.         dblHi = Hi
  41.     End If
  42.     CLargeInt = dblLo + dblHi * 2 ^ 32
  43. End Function
  44.  
Dec 5 '06 #10
sashi
1,754 Expert 1GB
Set default printer

Windows API/Global Declarations
Expand|Select|Wrap|Line Numbers
  1. Public Declare Function WriteProfileString Lib "kernel32" Alias "WriteProfileStringA" (ByVal lpszSection As String, ByVal lpszKeyName As String, ByVal lpszString As String) As Long
  2.  
  3. Public Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long
  4.     Public Const HWND_BROADCAST = &HFFFF&
  5.     Public Const WM_WININICHANGE = &H1A 
  6.  
Code
Expand|Select|Wrap|Line Numbers
  1. Public Function SetDefaultPrinter(objPrn As Printer) As Boolean
  2.     Dim x As Long, sztemp As String
  3.     sztemp = objPrn.DeviceName & "," & objPrn.DriverName & "," & objPrn.Port
  4.     x = WriteProfileString("windows", "device", sztemp)
  5.     x = SendMessage(HWND_BROADCAST, WM_WININICHANGE, 0&, "windows")
  6. End Function
  7.  
  8. Private Sub Command1_Click()
  9.     Dim x As Printer
  10.     If MsgBox("Are You Sure Want To Set " & Combo1.Text & " as Default printer ? ", vbYesNo, "Attention") = vbYes Then
  11.  
  12.         For Each x In Printers
  13.             If x.DeviceName = Combo1.Text Then
  14.                 SetDefaultPrinter x
  15.                 Exit Sub
  16.             End If
  17.         Next
  18.  
  19.     End If
  20. End Sub
  21.  
  22. Private Sub Form_Load()
  23.     Dim x As Printer
  24.     Dim y As Integer
  25.     y = 0
  26.  
  27.     With Combo1 'Scan all available printer and put them
  28.         For Each x In Printers 'in To combo box.
  29.             .AddItem x.DeviceName, y
  30.             y = y + 1
  31.         Next
  32.         .ListIndex = 0
  33.     End With
  34.  
  35. End Sub
  36.  
Dec 5 '06 #11
sashi
1,754 Expert 1GB
TabIndex

If your tabindex values are mixed up, I don't think VB6 gives you any convenient way to correct them - you have to edit them all (if I'm mistaken, I hope someone will point it out - I know MS Access has an option to sort them out).

The quickest way to do this is to start at the control you want to come last, then click on each of them in reverse sequence, and enter "0" for the TabIndex. You don't even have to hit enter, just click on the next one. So it's "0"-click-"0"-click-"0", very quick.

by Killer42
Dec 6 '06 #12
sashi
1,754 Expert 1GB
Database connectiong string

DB_Connection_String.zip
Dec 11 '06 #13
sashi
1,754 Expert 1GB
Windows system error lookup

Windows_System_Error_Lookup.zip
Dec 11 '06 #14
sashi
1,754 Expert 1GB
Hints for Visual Basic & LPT-1

VB - LPT Programming

by adeebraza
Dec 21 '06 #15
sashi
1,754 Expert 1GB
PC's Parallel Port Details & Visual Basic programming

VB - Parallel port programming

by adeebraza
Dec 21 '06 #16
sashi
1,754 Expert 1GB
Know LPT-1 Visual Basic Driver for Win 2000 & XP

VB - WinXP LPT programming

by adeebraza
Dec 21 '06 #17
Killer42
8,435 Expert 8TB
Execute VB code from a string



In VB5 or VB6, to execute VB code from a string (for example, a textbox) add the Microsoft Script Control under Project | Components.

Here is some sample code which shows one way to make use of it...
Expand|Select|Wrap|Line Numbers
  1. Private Sub Form_Load()
  2.   Text1.Text = "cmdText1.Visible = False"
  3.   Call ScriptControl1.AddObject(cmdText1.Name, cmdText1)
  4. End Sub
  5.  
  6. Private Sub Command1_Click()
  7.   Call ScriptControl1.ExecuteStatement(Text1.Text)
  8. End Sub
Further information is available by searching TheScripts or the entire web for “microsoft script control”.

By Steve Gerrard (guest) in Community > Newsgroup Archive > comp.lang.* > Visual Basic > How do I Execute the content of a textbox

Brought to the group’s attention by Phaneendra Varma
Mar 26 '07 #18

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Kenneth P | last post by:
Hi, I have a very good book on asp.net Sams ASP.NET Tips, Tutorials and Code ISBN 0-672-32143-2 and is trying to teach myself from examples from this book Chapter 16. Anyway I come quite...
1
by: code | last post by:
Hi Grp http://www.books-download.com/?Book=1493-PHP+Hacks+%3a+Tips+%26+Tools+For+Creating+Dynamic+Websites+(Hacks) Description Programmers love its flexibility and speed; designers love its...
0
by: travolta006 | last post by:
Learn how to optimize and tune up your system, get rid of boring errors and uninstall unnecessary thing with very useful tips and tricks http://windowsxpsp2pro.blogspot.com
6
by: travolta009 | last post by:
Learn how to optimize and tune up your system, get rid of boring errors and uninstall unnecessary thing with very useful tips and tricks http://windowsxpsp2pro.blogspot.com
68
bartonc
by: bartonc | last post by:
I've decide to compile a bunch of your favorite tips and tricks for the Articles section. I found a post yesterday by Chrisjc that is a perfect example. I copied his post over to create Dealing with...
2
bartonc
by: bartonc | last post by:
I've decide to compile a bunch of your favorite tips and tricks for the Articles section. Post your favorite tips and tricks here, in this thread, and I'll copy the best ones to a Tips and Tricks...
0
by: kamalpp | last post by:
hi check http://aspnet-tips-tricks.blogspot.com/ for tips and tricks Thanks
0
by: css | last post by:
http://php.apachai.com a lot of php tips & tricks
8
Frinavale
by: Frinavale | last post by:
Edit Many times we spend hours and hours trying to solve a problem. When we finally figure it out, we want to share it to keep others from suffering the same way! That's why we have this "Tips...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.