**Problem**:

Create a vb6 program that will ask the user to enter the total population then the program will compute the sample size.

**Sample size** – the number of respondents that will be the selected to answer the survey or questions.

Example: Total population = 1000, sample size = 100

Meaning out of 1000 only 100 will be part of the survey.

**Solution**:

To compute the sample **size,** we will use the slovin’s formula.

We can determine the number of respondents needed using this formula:

**n = N / (****1 + N e**^{2}**)**

Where:

n = is the sample **size**

**N = is the total population**

**e = is the margin of error (0.05) usual value**

Let’s start to code it

- Open your visual basic 6 program then select Standard EXE.
- A new project will be created with a Form1 on it.
- We need to add the following control or object to our form:
- 2 labels
- 2 textboxes
- 1 command button

- This is how we design the form.

Name the first textbox (with the label Enter Total number of Population) into txttotal.

The second textbox (with the label Respondents (sample size)) into txtsample.

- Double click the txttotal textbox and select the keypress event.

- Paste the code to the keypress event ot txttotal.

```
If (KeyAscii >= vbKey0 And KeyAscii <= vbKey9) Or KeyAscii = 46 Or KeyAscii = vbKeyBack Then
Else
Beep
KeyAscii = 0
End If
```

**Code Explanation**:

The code above will prevent the user to enter a non-numeric value to the textbox.

- Double click again the txttotal and this time select the change event and paste this code.

```
txtsample.Text = Val(txttotal.Text) / (1 + (Val(txttotal.Text) * 0.0025))
```

**Code Explanation**:

The code above will compute the sample size whenever the user has entered the population in the txttotal textbox.

- To close the form double click the Close button and type the code

```
Unload me
```

- Save and Run the project.