To calculate a 95% confidence interval for p with a margin of error no greater than d, use the formula
n = (1.962)(p)(q) / d2
If no reasonable estimate for p exists, assume p = 0.5, to ensure an adequate sample size.
You may use EpiTable | Sample | Sample Size | Single Proportion to automate this calculations.
When sampling from a finite population without replacement or when using more complex random sampling methods (e.g., cluster sampling), the formula must be adjusted.