Results 1 to 40 of 98

Thread: good old arrays

Hybrid View

  1. #1
    Head Hunted anhn's Avatar
    Join Date
    Aug 2007
    Location
    Australia
    Posts
    3,669

    Re: good old arrays

    Try this:
    Code:
    Option Explicit
    
    Private Type Student
      strName As String
      intMark As Integer
    End Type
    
    Dim Students() As Student
    
    Private Sub cmdAddRecord_Click()
        Dim iMark As Long
        Dim sName As String
        Dim i As Long
        
        sName = Trim(txtStudentName.Text)
        If sName = "" Then
            MsgBox "student name cannot be blank. Please re-enter it"
            Exit Sub
        End If
    
        iMark = Val(txtStudentMark.Text)
        If iMark < 0 Then
            MsgBox "student mark cannot be less than zero. Please re-enter it"
            Exit Sub
        ElseIf iMark > 100 Then
            MsgBox "student mark cannot be greater than 100. Please re-enter it"
            Exit Sub
        End If
        
        If (Not Students) = -1 Then '-- Students() array was not initialized
            i = 1
        Else
            i = UBound(Students) + 1
        End If
        ReDim Preserve Students(1 To i)
        
        Students(i).strName = sName
        Students(i).intMark = iMark
           
        lstNames.AddItem sName
        lstMarks.AddItem iMark
    
    End Sub
    • Don't forget to use [CODE]your code here[/CODE] when posting code
    • If your question was answered please use Thread Tools to mark your thread [RESOLVED]
    • Don't forget to RATE helpful posts

    • Baby Steps a guided tour
    • IsDigits() and IsNumber() functions • Wichmann-Hill Random() function • >> and << functions for VB • CopyFileByChunk

  2. #2
    Freelancer akhileshbc's Avatar
    Join Date
    Jun 2008
    Location
    Trivandrum, Kerala, India
    Posts
    7,652

    Re: good old arrays

    Quote Originally Posted by anhn View Post
    Try this:
    Code:
    Option Explicit
    
    Private Type Student
      strName As String
      intMark As Integer
    End Type
    
    Dim Students() As Student
    
    Private Sub cmdAddRecord_Click()
        Dim iMark As Long
        Dim sName As String
        Dim i As Long
        
        sName = Trim(txtStudentName.Text)
        If sName = "" Then
            MsgBox "student name cannot be blank. Please re-enter it"
            Exit Sub
        End If
    
        iMark = Val(txtStudentMark.Text)
        If iMark < 0 Then
            MsgBox "student mark cannot be less than zero. Please re-enter it"
            Exit Sub
        ElseIf iMark > 100 Then
            MsgBox "student mark cannot be greater than 100. Please re-enter it"
            Exit Sub
        End If
        
        If (Not Students) = -1 Then '-- Students() array was not initialized
            i = 1
        Else
            i = UBound(Students) + 1
        End If
        ReDim Preserve Students(1 To i)
        
        Students(i).strName = sName
        Students(i).intMark = iMark
           
        lstNames.AddItem sName
        lstMarks.AddItem iMark
    
    End Sub
    I had already suggested and posted the code for using dynamic array in post #21

    If my post was helpful to you, then express your gratitude using Rate this Post.
    And if your problem is SOLVED, then please Mark the Thread as RESOLVED (see it in action - video)
    My system: AMD FX 6100, Gigabyte Motherboard, 8 GB Crossair Vengance, Cooler Master 450W Thunder PSU, 1.4 TB HDD, 18.5" TFT(Wide), Antec V1 Cabinet

    Social Group: VBForums - Developers from India


    Skills: PHP, MySQL, jQuery, VB.Net, Photoshop, CodeIgniter, Bootstrap,...

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