Results 1 to 2 of 2

Thread: General Problem For new User

  1. #1

    Thread Starter
    Just Married shakti5385's Avatar
    Join Date
    Mar 2006
    Location
    Udaipur,Rajasthan(INDIA)
    Posts
    3,747

    Arrow General Problem For new User

    This the code bank for the newly appear user who face some common problem always or starting.

    1) Only Numeric value in the text box

    VB Code:
    1. Function OnlyNum(Key As Integer)
    2. If Key <> 45 And Key <> 8 Then
    3. If Not IsNumeric(Chr(Key)) Then
    4. Key = 0
    5. End If
    6. End If
    7. End Function
    8.  
    9. Private Sub Text1_KeyPress(KeyAscii As Integer)
    10. Call OnlyNum(KeyAscii)
    11. End Sub

    2) Only Alphabetic in the text box
    VB Code:
    1. Function OnlyAlphabets(Key As Integer)
    2. If Not (Key >= 65 And Key <= 123 Or Key = 8 Or Key = 32) Then
    3. Key = 0
    4. End If
    5. End Function
    6.  
    7. Private Sub Text1_KeyPress(KeyAscii As Integer)
    8. Call OnlyAlphabets(KeyAscii)
    9. End Sub

    3) Remove Special character
    VB Code:
    1. Function SpecialCharacter(Key As Integer)
    2. If ((Key < 65 Or Key > 93) And (Key < 97 Or Key > 125) And (Key < 48 Or Key > 59) And (Key <> 41) And (Key <> 40) And (Key <> 44) And (Key <> 45) And (Key <> 46) And (Key <> 32) And (Key <> 8)) Then
    3. Key = 0
    4. 'Special Character are not Allowed
    5. End If
    6. End Function
    7.  
    8. Private Sub Text1_KeyPress(KeyAscii As Integer)
    9. Call SpecialCharacter(KeyAscii)
    10. End Sub

    4) Clear all controls on the forums
    VB Code:
    1. Public Sub ClearAllText(frm As Form)
    2. Dim ctl As Control
    3. For Each ctl In frm.Controls
    4.     If TypeOf ctl Is TextBox Then: ctl.Text = ""
    5.     If TypeOf ctl Is ComboBox Then: ctl.ListIndex = -1
    6.     If TypeOf ctl Is OptionButton Then: ctl.Value = False
    7.     If TypeOf ctl Is CheckBox Then: ctl.Value = 0
    8. Next ctl
    9. Exit Sub
    10. End Sub
    11.  
    12. Private Sub Form_Load()
    13. Call ClearAllText(Me)
    14. End Sub

    5) Clear particular control
    VB Code:
    1. Public Sub Clear_Particular_Control(frm As Form, F As Frame)
    2. 'Code For Crearing Particular Control That is In the Frame
    3. Dim Ctr As Control
    4. For Each Ctr In frm.Controls
    5. If TypeOf Ctr Is TextBox Then
    6. If Ctr.Container = F Then
    7.     Ctr.Text = ""
    8. End If
    9. End If
    10. Next
    11. End Sub
    12.  
    13. Private Sub Command1_Click()
    14. Call Clear_Particular_Control(Me, Frame1)
    15. End Sub

    6) Check Only One decimal in the textbox
    VB Code:
    1. Public Sub Check_Decimal(T As TextBox, Key As Integer)
    2. 'Controls - How to allow only number and only one occurence of decimal character in text box
    3. Select Case Key
    4. Case Is < 32               ' Control keys are OK.
    5. Case 46
    6. If InStr(1, T.Text, ".") <> 0 Then Key = 0
    7. Case 48 To 57              ' This is a digit.
    8. Case Else                  ' Reject any other key.
    9. Key = 0
    10. End Select
    11. End Sub
    12.  
    13. Private Sub Text1_KeyPress(KeyAscii As Integer)
    14. Call Check_Decimal(Text1, KeyAscii)
    15. End Sub

    7) Print Any Grid
    VB Code:
    1. Public Sub PrintGrid(MyGrid As MSFlexGrid)
    2. ‘Change the type of the Grid in you code in definition of the function
    3. On Error Resume Next
    4. Dim OldWidth As Integer
    5.  
    6. OldWidth = MyGrid.Width
    7. MyGrid.Width = Printer.Width
    8. Printer.PaintPicture MyGrid.Picture, 0, 0
    9. Printer.EndDoc
    10. MyGrid.Width = OldWidth
    11. End Sub
    12.  
    13. Private Sub Command1_Click()
    14. Call PrintGrid(MSFlexGrid1)
    15. End Sub

    8) Convert Grid Data to Excel
    VB Code:
    1. Public Sub FlexToExcel(MyGrid As MSFlexGrid)
    2. Dim xlObject    As Excel.Application
    3. Dim xlWB        As Excel.Workbook
    4. Set xlObject = New Excel.Application
    5. 'This Adds a new woorkbook, you could open the workbook from file also
    6. Set xlWB = xlObject.Workbooks.Add
    7.  
    8. Clipboard.Clear 'Clear the Clipboard
    9. With MyGrid
    10. 'Select Full Contents (You could also select partial content)
    11.     .Col = 0               'From first column
    12.     .Row = 0               'From first Row (header)
    13.     .ColSel = .Cols - 1    'Select all columns
    14.     .RowSel = .Rows - 1    'Select all rows
    15.     Clipboard.SetText .Clip 'Send to Clipboard
    16. End With
    17.  
    18. With xlObject.ActiveWorkbook.ActiveSheet
    19.     .Range("A1").Select 'Select Cell A1 (will paste from here, to different cells)
    20.     .Paste              'Paste clipboard contents
    21. End With
    22.  
    23. ' This makes Excel visible
    24. xlObject.Visible = True
    25. End Sub
    26.  
    27. Private Sub Command1_Click()
    28. Call FlexToExcel(MSFlexGrid1)
    29. End Sub

    9) Center Position Of the form
    VB Code:
    1. Public Sub Cent(frm As Form)
    2. X = (Screen.Width - frm.Width) / 2
    3. frm.Top = 0
    4. frm.Move X
    5. End Sub
    6.  
    7. Private Sub Form_Load()
    8. Call Cent(Me)
    9. End Sub

    10) Database connection
    VB Code:
    1. Public Con  As New ADODB.Connection
    2. Function Open_Connection()
    3. If Con.State = 1 Then
    4.     Con.Close
    5. End If
    6. With Con
    7.     .ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & App.Path & "\DatabaseName.mdb;Persist Security Info=False"
    8.     'Change the database Name
    9.     .ConnectionTimeout = 30
    10.     .CursorLocation = adUseClient
    11.     .Open
    12. End With
    13. End Function
    14.  
    15. Private Sub Form_Load()
    16. Call Open_Connection
    17. End Sub
    To Be Continued........
    Last edited by shakti5385; Sep 28th, 2006 at 03:44 AM.

  2. #2

    Thread Starter
    Just Married shakti5385's Avatar
    Join Date
    Mar 2006
    Location
    Udaipur,Rajasthan(INDIA)
    Posts
    3,747

    Thumbs up Re: General Problem For new User

    1) Add Dsn run Time

    VB Code:
    1. Option Explicit
    2. Public Const ODBC_ADD_SYS_DSN = 4
    3. Public Const ODBC_REMOVE_SYS_DSN = 6
    4. Public Const VbapiNull = 0
    5.  
    6. Public x As Integer
    7. Public Con As New ADODB.Connection
    8. Public strdriver As String
    9. Public strAttributes As String
    10. Public Declare Function sqlconfigdatasource Lib "ODBCCP32.DLL" Alias "SQLConfigDataSource" (ByVal hundparent As Long, ByVal frequest As Long, ByVal lpszDriver As String, ByVal lpszattributes As String) As Long
    11. Public inet As Long
    12. Sub Main()
    13. strdriver = "Microsoft Access Driver (*.mdb)"
    14. strAttributes = "DSN=Ifw" & Chr$(0) 'This Make a dsn Name ifw
    15. strAttributes = strAttributes & "DBQ=" & App.Path & "\Salary.mdb" & Chr$(0) 'Salary.mdb is the database name
    16. strAttributes = strAttributes & "PWD=5385" '5385 is the password of the database
    17. inet = sqlconfigdatasource(VbapiNull, ODBC_ADD_SYS_DSN, strdriver, strAttributes)
    18. If inet Then
    19. Else
    20. Exit Sub
    21. End If
    22. Con.CursorLocation = adUseClient
    23. Con.Open "DSN=ifw"
    24. Form1.Show
    25. End Sub

    To be continued..

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