home *** CD-ROM | disk | FTP | other *** search
/ Visual Basic 6 Unleashed…sional Reference Edition) / Visual_Basic_6_Unleashed_Professional_Reference_Edition_Sams_1999.iso / Source / CHAP29 / 309X3202.TXT < prev    next >
Encoding:
Text File  |  1998-05-05  |  1.5 KB  |  57 lines

  1.  
  2. Private Sub cmdAdd_Click()
  3.  
  4. ' Make sure the user entered a section name.
  5. If txtSectionName.Text = "" Then
  6.     MsgBox "Please enter a section name first."
  7.     Exit Sub
  8. End If
  9.  
  10. ' For INI files, call the INIAdd function. For
  11. ' the Registry, call the RegAdd function.
  12. If optType(0).Value = True Then
  13.     lblStatus = INIAdd()
  14. Else
  15.     lblStatus = RegAdd()
  16. End If
  17.  
  18. End Sub
  19.  
  20.  
  21. Private Function INIAdd() As String
  22.  
  23. Dim lonStatus As Long
  24. Dim strSectionName As String
  25.  
  26. ' Remove brackets from section name, if necessary.
  27. strSectionName = FixSectionName(txtSectionName.Text)
  28.  
  29. If txtKeyName.Text = "" Then
  30.     ' No key name was specified, so add a new (empty)
  31.     ' section with the WritePrivateProfileSection
  32.     ' API function.
  33.     lonStatus = WritePrivateProfileSection(strSectionName, _
  34.         "", dlgGetFilename.FileName)
  35.     ' Determine the status of the operation.
  36.     If lonStatus = 0 Then
  37.         INIAdd = "Error - Section could not be added"
  38.     Else
  39.         INIAdd = "Section added succesfully"
  40.         ReadFile
  41.     End If
  42. Else
  43.     ' Use the WritePrivateProfileString API function
  44.     ' to add the key and, if necessary, the section.
  45.     lonStatus = WritePrivateProfileString(strSectionName, _
  46.         txtKeyName.Text, txtValue.Text, dlgGetFilename.FileName)
  47.     ' Determine the status of the operation.
  48.     If lonStatus = 0 Then
  49.         INIAdd = "Error - Key could not be added"
  50.     Else
  51.         INIAdd = "Key added successfully"
  52.         ReadFile
  53.     End If
  54. End If
  55.  
  56. End Function
  57.